2017-12-27 22 views
0

私は次の形式でXMLを消費しなければならないRESTコールバックサービスがあります。Spring REST:ネストされたXMLリクエストボディの適切なコンストラクタ?

<SearchRequest> 
    <SearchCriteria> 
    <Param1></Param2> 
    <Param2></Param2> 
    </SearchCriteria> 
</SearchRequest> 

実際のXMLは、「基準」内の約32のパラメータを持っているが、これは基本的なアイデアを提供します。

属性searchCriteriaとparam1とparam2を持つSearchCriteriaクラスを持つSearchRequestクラスを作成しました。

マイRESTコントローラクラスは、次のようになります。私は、上記のサービスをテストする場合

import org.springframework.beans.factory.annotation.Value; 
import org.springframework.http.HttpStatus; 
import org.springframework.http.ResponseEntity; 
import org.springframework.web.bind.annotation.RequestBody; 
import org.springframework.web.bind.annotation.RequestHeader; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RestController; 

@RestController 
@RequestMapping("/acme/request/search") 
public class AcmeCallbackController { 
    @RequestMapping(method = RequestMethod.POST, consumes = "application/xml") 
    public ResponseEntity<String> postAcmeSearch(@RequestBody SearchRequest body) { 
     StringBuffer resultBuffer = new StringBuffer(2048); 
     // implementation code here, 'body' now expected to be a SearchRequest object contructed from request body XML 
     return new ResponseEntity<String>(resultBuffer.toString(), HttpStatus.OK); 
    } 

は、私は、次のエラーレスポンスを受信します。

`{ "timestamp": 1514390248822, 
"status": 400, 
"error": "Bad Request", 
"exception": org.springframework.http.converter.HttpMessageNotReadableException", 
"message": "JSON parse error: Can not construct instance of SearchRequest: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of SearchRequest: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)\n at [Source: [email protected]; line: 2, column: 3]", 
"path": "/acme/request/search" }` 

誰もが、適切なコンストラクタおよび/または注釈を知っていますSearchRequestに適用すると、XMLリクエストが正しくデシリアライズされますか?私はすべてのgetterとsetterに@JsonProperty( "{Attribute}")を持っています。{Attribute}はXML要素名と一致する最初の上限を持つ属性の名前です。各属性値の引数を持つコンストラクタ。

TIA、 エド

+0

SearchRequestクラスを共有できますか?クラスにパラメータ化されたコンストラクタが定義されていますか?はいの場合はデフォルトのコンストラクタも追加します。 –

+0

https://stackoverflow.com/questions/7625783/jsonmappingexception-no-suitable-constructor-found-for-type-simple-type-class?rq=1、https:// stackoverflowなどのJsonMappingExceptionsのソリューションを探します。 .com/questions/12750681/can-construct-instance-of-jackson – tkruse

答えて

0

私はそれを考え出しました。コンストラクタの引数にアノテーションを追加する必要がありました。

public SearchRequest(@JsonProperty("Param1") String param1, 
     @JsonProperty("Param2") String param2) { 
    this.param1 = param1; 
    this.param2 = param2; 
} 

その後、正常に機能しました。

関連する問題