首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SpringBoot:使用RestTemplate调用查询

SpringBoot:使用RestTemplate调用查询
EN

Stack Overflow用户
提问于 2016-12-22 02:37:36
回答 0查看 626关注 0票数 2

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

代码语言:javascript
复制
@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“:

代码语言:javascript
复制
@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);
}
}

该属性如下所示:

代码语言:javascript
复制
@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}“url时,我得到以下错误:

代码语言:javascript
复制
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调用查询?还是有更好的方法呢?

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41269701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档