使用Spring从存储库中获取数据

时间:2016-06-28 23:51:53

标签: java spring jpa repository

好的,所以我是春天新手,不知道这是怎么回事。我一直在尝试一些事情,并认为它接近这样做但没有从服务器获取任何数据并给我这个错误

 Unsatisfied dependency expressed through constructor argument with index 4 of type [jp.co.fusionsystems.dimare.crm.service.impl.MyDataDefaultService]: : Error creating bean with name 'MyDataDefaultService' defined in file  

我的终点

//mobile data endpoint
 @RequestMapping(
        value = API_PREFIX + ENDPOINT_MyData + "/getMyData",
        method = RequestMethod.GET)
public MyData getMyData() {
    return MyDataDefaultService.getData();
}

我的对象

public class MyData {

public MyData(final Builder builder) {
    videoLink = builder.videoLink;
}
private String videoLink;

public String getVideoLink()
{
    return videoLink;
}

public static class Builder
{
    private String videoLink = "";  

    public Builder setVideo(String videoLink)
    {
        this.videoLink = videoLink;
        return this;
    }

    public MyData build()
    {
        return new MyData(this);
    }

}
 @Override
    public boolean equals(final Object other) {
        return ObjectUtils.equals(this, other);
    }

    @Override
    public int hashCode() {
        return ObjectUtils.hashCode(this);
    }

    @Override
    public String toString() {
        return ObjectUtils.toString(this);
    }

    }

存储库

 public classMyServerMyDataRepository implements MyDataRepository{

private finalMyServerMyDataJpaRepository jpaRepository;
private final MyDataConverter MyDataConverter = new MyDataConverter();

@Autowired
publicMyServerMyDataRepository(finalMyServerMyDataJpaRepository jpaRepository) {
    this.jpaRepository = Validate.notNull(jpaRepository);
}


@Override
public MyData getData() {
    MyDataEntity entity = jpaRepository.findOne((long) 0);
    MyData.Builder builder = new MyData.Builder()
            .setVideo(entity.getVideoLink());

    return builder.build();
}

端点

调用的DefaultService
 public class MyDataDefaultService {
private static final Logger logger =      LoggerFactory.getLogger(NotificationDefaultService.class);

private finalMyServerMyDataRepository repository;

@Autowired
public MyDataDefaultService(MyServerMyDataRepository repository) {
    this.repository = Validate.notNull(repository);
}


//Get the data from the server
public MobileData getData()
{
    logger.info("Get Mobile Data from the server");
    //Get the data from the repository
    MobileData mobileData = repository.getData();

    return mobileData;

}
 }

转换器

 public class MyDataConverter  extends AbstractConverter<MyDataEntity, MyData>
 {

@Override
public MyData convert(MyDataEntity entity) {

    MyData.Builder builder = new MyData.Builder()
            .setVideo(entity.getVideoLink());
    return builder.build();
}

 }

我的实体

 @Entity
 @Table(name = “myServer”)
 public class MyDataEntity extends AbstractEntity{
 @Column(name = "video_link", nullable = true)
    private String videoLink;

 public String getVideoLink() {
        return videoLink;
    }

    public void setVideoLink(final String videoLink) {
        this.videoLink = videoLink;
    }

}

感谢您对此提供任何帮助

1 个答案:

答案 0 :(得分:2)

Hibernate实体应该定义默认构造函数并实现Serializable接口,假设AbstractEntity是。 Hibernate不会接受没有主键的实体,所以你也必须定义它:

 @Entity
 @Table(name = “myServer”)
 public class MyDataEntity implements Serializable {

    @Id
    @GeneratedValue
    private Long id;

    @Column(name = "video_link", nullable = true)
    private String videoLink;

    public MyDataEntity() {

    }

    ...setters&getters
}

MyData对象表示JSON服务器响应,您可以使用Jackson注释来控制结果JSON属性:

public class MyDataResponse {

    @JsonProperty("video_link")
    private String videoLink;

    public MyDataResponse() {

    }

    public MyDataResponse(String videoLink) {
        this.videoLink = videoLink;
    }

    ...setters&getters
}

Spring有一个名为Spring Data的令人敬畏的项目,它提供了JPA存储库,不需要任何组件注释:

public class MyDataRepository extends CrudRepository<MyDataEntity, Long> {

}

Builder类代表服务层:

@Service
public class MyDataService {

    @Autowired
    private MyDataRepository myDataRepository;

    public MyDataResponse getMyData(Long id) {

        MyDataEntity entity = myDataRepository.findOne(id);
        ...rest logic, copy necessary data to MyDataResponse
    } 
}

然后控制器是:

@RestController // @ResponseBody not needed when using like this
public MyDataController {

    @Autowired
    private MyDataService myDataService;

    @RequestMapping("/getMyData") // no need to specify method for GET
    public MyDataResponse getMyData(@RequestParam("ID") Long myDataId) {

            ... validation logic

            return myDataService.getMyData(myDataId); // return response
    }
}

现在它应该可以工作,不要忘记在类路径中添加所需的依赖项。