방금 스프링 부트를 사용하기 시작했으며 RestTemplate을 사용하여 쿼리를 호출하고 그 결과를 반환하고 싶습니다.SpringBoot : RestTemplate을 사용하여 쿼리 호출
@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);
}
}
속성은 다음과 같습니다 :이 RestController에 쿼리 "findByMatchingCriteria를"전화 드렸습니다
@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);
}
는
@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/{부동산 ID}을 테스트 할 때 "다음 오류가 발생합니다.
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: [email protected]; 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: [email protected]; line: 1, column: 1]
RestTemplate을 사용하여 쿼리를 호출하려면 어떻게해야합니까? 아니면 더 좋은 방법이 있을까요?
컨트롤러와 저장소가 같은 프로젝트에 있습니까? – Adam
저장소를 엔드 포인트로 공개 했습니까? – Gregg
@ Adam 그들은 같은 프로젝트에 있지만 다른 마이크로 서비스에 있습니다. – Milebza