java - SpringBoot : call a query using RestTemplate

标签 java list spring-boot resttemplate

我刚刚开始使用 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 - SpringBoot : call a query using RestTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41269701/

相关文章:

python - 加入两个列表以创建一个新列表

python - 迭代字符串列表,直到 string == newline

java - Spring Boot 中 @oneToMany 双向映射的问题

java - 带有嵌入式 H2GIS 的 Spring Boot 应用程序 - 初始化引发 SQL 语法错误

java - 我如何在远程服务器上部署jar文件

java - Java 17 中的 LineNumberReader 是否存在不兼容性?

java - 为什么源代码中的根包名为 "com"?

list - haskell:调用具有不同类型列表的函数

java - 如何限制jtable的行大小?

Java - 在 FTP 服务器上存储文件失败