0
我有一個REST回調服務必須按以下格式使用XML:Spring REST:適用於嵌套XML請求正文的構造函數嗎?
<SearchRequest>
<SearchCriteria>
<Param1></Param2>
<Param2></Param2>
</SearchCriteria>
</SearchRequest>
實際的XML具有「標準」的範圍內約32個參數,但是這給了基本思路。
我創建了一個SearchRequest類,它具有屬性searchCriteria和具有param1和param2的SearchCriteria類。
我的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, 埃德
你可以分享你的SearchRequest類,你有沒有在類中定義的任何參數化構造函數,如果是,那麼也添加默認構造函數。 –
尋找JsonMappingExceptions的解決方案,就像https://stackoverflow.com/questions/7625783/jsonmappingexception-no-suitable-constructor-found-for-type-simple-type-class?rq=1,https:// stackoverflow .COM /問題/ 12750681 /不能-構建實例 - 的 - 傑克遜 – tkruse