首页 > 解决方案 > 使用java spring rest api并将记录插入mysql表

问题描述

我正在尝试使用 spring rest API 并将结果存储到临时表中。这是我尝试过的。我对java和spring很陌生,请原谅我的错误。我的临时表名称“greetingsstaging”。我是春季开发的新手,请帮助理解这个问题。我正在使用 JPA 和 spring。

这是主要的应用程序。

package com.javatechie.spring.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.javatechie.spring.api.entty.Greetings;
import com.javatechie.spring.api.service.Greet;
import com.javatechie.spring.api.service.GreetInterface;
import com.javatechie.spring.api.service.GreetingsService;

@SpringBootApplication
public class GreetingsConsumerApplication {
    private static RestTemplate rs = new RestTemplate();
    private static String baseUrl = "http://localhost:8080/api/Greetings";

    private static GreetInterface greetInterface;

    @Autowired
    public GreetingsConsumerApplication(GreetInterface theGreetInterface) {
        greetInterface = theGreetInterface;
    }

    public static void main(String[] args) {
        SpringApplication.run(GreetingsConsumerApplication.class, args);

        ResponseEntity<List<GreetingsService>> response = rs.exchange(baseUrl, HttpMethod.GET, null,
                new ParameterizedTypeReference<List<GreetingsService>>() {
                });
        List<GreetingsService> gs = response.getBody();

        for (GreetingsService g : gs) {

            Greetings greetings = new Greetings(g.getId(), g.getName(), g.getAge(), g.getAddress());
            System.out.println("Id : " + greetings.getId() + " name : " + greetings.getName() + " Age : "
                    + greetings.getAge() + " Address : " + greetings.getAddress());
            greetInterface.save(greetings);
        }

    }

}

这是我的服务。

package com.javatechie.spring.api.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.javatechie.spring.api.entty.Greetings;
import com.javatechie.spring.api.repository.GreetingsRepository;

@Service
public class Greet implements GreetInterface{

    private GreetingsRepository repo;

    @Autowired
    public Greet(GreetingsRepository repo) {
        super();
        this.repo = repo;
    }
    @Override
    public void save(Greetings obj) {
        repo.save(obj);

    }
}

这是存储库界面。

package com.javatechie.spring.api.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.javatechie.spring.api.entty.Greetings;

@Repository
public interface GreetingsRepository extends JpaRepository<Greetings, Integer> {

}

这是我的 pojo 课。

package com.javatechie.spring.api.entty;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="greetingsstaging")
public class Greetings {

    int id;
    String name;
    int age;
    String address;

    public Greetings(int id, String name, int age, String address) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "Greetings [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + "]";
    }

}

我收到以下异常。

在类路径资源 [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class] 中定义名称为“requestMappingHandlerAdapter”的 bean 创建错误:通过方法“requestMappingHandlerAdapter”参数 1 表达的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class] 中定义名称为“mvcConversionService”的 bean 创建错误:通过工厂进行 Bean 实例化方法失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.format.support.FormattingConversionService]:工厂方法“mvcConversionService”抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在创建 com.javatechie.spring.api.repository.GreetingsRepository 中定义的名称为“greetingsRepository”的 bean 时出错jpaMappingContext' 同时设置 bean 属性'mappingContext'; 嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“jpaMappingContext”的 bean 时出错:调用 init 方法失败;嵌套异常是 org.hibernate.AnnotationException:没有为实体指定标识符:com.javatechie.spring.api.entty.Greetings 在 JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration 上声明的@EnableJpaRepositories 中定义的com.javatechie.spring.api.repository.GreetingsRepository 中定义的greetingsRepository:无法解析对bean 'jpaMappingContext' 的引用,同时设置bean 属性'mappingContext';嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“jpaMappingContext”的 bean 时出错:调用 init 方法失败;嵌套异常是 org.hibernate.AnnotationException:没有为实体指定标识符:com.javatechie.spring.api.entty.Greetings 在 JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration 上声明的@EnableJpaRepositories 中定义的com.javatechie.spring.api.repository.GreetingsRepository 中定义的greetingsRepository:无法解析对bean 'jpaMappingContext' 的引用,同时设置bean 属性'mappingContext';嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“jpaMappingContext”的 bean 时出错:调用 init 方法失败;嵌套异常是 org.hibernate.AnnotationException:没有为实体指定标识符:com.javatechie.spring.api.entty.Greetings 同时设置bean属性'mappingContext';嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“jpaMappingContext”的 bean 时出错:调用 init 方法失败;嵌套异常是 org.hibernate.AnnotationException:没有为实体指定标识符:com.javatechie.spring.api.entty.Greetings 同时设置bean属性'mappingContext';嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“jpaMappingContext”的 bean 时出错:调用 init 方法失败;嵌套异常是 org.hibernate.AnnotationException:没有为实体指定标识符:com.javatechie.spring.api.entty.Greetings

我的主要想法是将 API 的结果存储到临时表中。

标签: javaspringspring-boothibernatejpa

解决方案


您缺少 Greetings 类的 @Id 注释。每个 JPA 实体都必须有一个唯一标识它的主键。

@Entity
@Table(name="greetingsstaging")
public class Greetings {

    @Id
    int id;
}

推荐阅读