gpt4 book ai didi

java - 使用 Spring 从存储库获取数据

转载 作者:行者123 更新时间:2023-12-02 03:31:03 24 4
gpt4 key购买 nike

好吧,我是 Spring 新手,不太知道它是如何工作的。我一直在尝试一些事情,并认为它接近做到这一点,但没有从服务器获取任何数据并给我这个错误

 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;
}

}

感谢您对此提供的任何帮助

最佳答案

Hibernate 实体应该定义默认构造函数并实现 Serialized 接口(interface),假设 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 存储库,因此甚至不需要 @Repository 注释:

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
}
}

那么 Controller 是:

@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
}
}

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

关于java - 使用 Spring 从存储库获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38088095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com