I have a REST callback service that must consume XML in the following format:
<SearchRequest>
<SearchCriteria>
<Param1></Param2>
<Param2></Param2>
</SearchCriteria>
</SearchRequest>
The actual XML has about 32 parameters within "Criteria", but this gives the basic idea.
I created a SearchRequest class that has the attribute, searchCriteria, and a SearchCriteria class that has param1 and param2.
My REST controller class looks something like this:
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);
}
When I test the above service I receive the following error response:
`{ "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: java.io.PushbackInputStream@26bd9717; line: 2, column: 3]",
"path": "/acme/request/search" }`
Does anyone know the suitable constructor and / or annotations to apply to SearchRequest, so that the xml request is correctly deserialized? I have @JsonProperty("{Attribute}") on all the getters and setters, where {Attribute} is the name of the attribute with an initial cap to match the XML element name, and a constructor that has an argument for each attribute value.
TIA, Ed