2016-12-21 1 views
1

私はSpringBootの使用を開始しましたが、RestTemplateを使用してクエリを呼び出し、結果を返したいとします。私はこのRestControllerで "findByMatchingCriteria" クエリを呼んでいるSpringBoot: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); 
} 

:私は「matchingByProperty/{propertyId}をテストする場合

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

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

プロパティは次のようになります"私は次のエラーが表示されます:

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を使用してクエリを呼び出すにはどうすればよいですか?それとももっと良い方法がありますか?

+0

コントローラとリポジトリは同じプロジェクトにありますか? – Adam

+0

リポジトリをエンドポイントとして公開しましたか? – Gregg

+0

@Adam彼らは同じプロジェクトに入っていますが、異なるマイクロサービスに入っています。 – Milebza

答えて

1

は解決策を考え出しました私の問題のために。

はApplicantRepositoryのクエリを呼び出すApplicantControllerにおけるREST方式作成:

@RequestMapping(method = POST, value = "/api/applicants/getMatchedApplicants") 
public List getMatchedApplicants(@RequestBody HashMap property) { 

    List<Applicant> applicants = applicantRepository.matchedApplicants((int) property.get("typeId"), 
                     (int) property.get("bedrooms"), 
                     (int) property.get("price")); 

    return applicants == null ? new ArrayList() : applicants; 
} 

をそしてMatchingControllerはRESTでApplicantControllerでメソッドを呼び出します。

これは、RESTによって通信する2つの異なるサービス(マッチングと応募者)が欲しかったものです。

ありがとうございました@あなたの提案が助けてくれました。

1

コントローラーから自分のリポジトリを呼び出す場合は、RestTemplateでHTTPを実行する必要はありません。単にあなたのコントローラにリポジトリを注入し、メソッドを呼び出します。

@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

+0

マイクロサービスを使用していてリポジトリがコントローラとは異なるサービスにあるため、HTTPを使用してクエリを呼び出すことを選択しました。私はカプセル化され、安心してコミュニケートしたい – Milebza

関連する問題