gpt4 book ai didi

java - Spring 启动: call a query using RestTemplate

转载 作者:行者123 更新时间:2023-11-30 08:30:22 24 4
gpt4 key购买 nike

我刚开始使用 Spring Boot,我想使用 RestTemplate 来调用查询并返回其结果。

@Repository
public interface ApplicantRepository extends CrudRepository<Applicant, Integer> {

@Query("SELECT a FROM Applicant a WHERE a.propertyTypeId = :typeId" +
" AND a.bedrooms = :bedrooms" +
" AND a.budget = :price")
List<Applicant> findByMatchingCriteria(@Param("typeId")int typeId, @Param("bedrooms") int bedrooms,
@Param("price") int price);
}

我在这个 RestController 中调用查询“findByMatchingCriteria”:

@RestController
public class MatchingController {

private RestTemplate restTemplate = new RestTemplate();

@RequestMapping(method = GET, value = "matchingByProperty/{propertyId}")
public List matchingByProperty(@PathVariable int propertyId) {
Property property = restTemplate.getForObject("http://localhost:8080/api/properties/" + propertyId, Property.class);

return restTemplate.getForObject("http://localhost:8080/api/applicants/search/findByMatchingCriteria?typeId=" + property.getTypeId()
+ "&bedrooms=" + property.getBedrooms()
+ "&price=" + property.getPrice(), List.class);
}
}

属性看起来像这样:

@Entity
public class Property {
private @Id @GeneratedValue int id;
private String fullAddress;
private int typeId;
private int subtypeId;
private int bedrooms;
private int price;

private Property() {}

public Property(String fullAddress, int typeId, int subtypeId, int bedrooms, int price) {
this.fullAddress = fullAddress;
this.typeId = typeId;
this.subtypeId = subtypeId;
this.bedrooms = bedrooms;
this.price = price;
}
// getters and setters
}

当我测试“matchingByProperty/{propertyId}”网址时,出现以下错误:

exec-5] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP     message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
at [Source: java.io.PushbackInputStream@3bd79387; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
at [Source: java.io.PushbackInputStream@3bd79387; line: 1, column: 1]

如何使用 RestTemplate 调用查询?或者有更好的方法吗?

最佳答案

如果您只想从 Controller 调用您自己的存储库,则无需使用 RestTemplate 通过 HTTP。只需将存储库注入(inject)您的 Controller ,然后调用方法:

@RestController
public class MatchingController {

@Autowired
private ApplicantRepository applicantRepository;

@RequestMapping(method = GET, value = "matchingByProperty/{propertyId}")
public List matchingByProperty(@PathVariable int propertyId) {
// You probably should not be going over HTTP for this either
// and autowiring a property repository.
Property property = restTemplate.
getForObject("http://localhost:8080/api/properties/" + propertyId, Property.class);

return applicantRepository.findByMatchingCriteria(property.getTypeId(), property.getBedrooms(), property.getPrice());
}
}

如果出于某种原因你确实想通过 HTTP,你可以使用这里的方法:Get list of JSON objects with Spring RestTemplate

关于java - Spring 启动: call a query using RestTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41269701/

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