0

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

  • Can you share your SearchRequest class, do you have any parameterized constructor defined in the class, if yes then add default constructor as well. – Amit K Bist Dec 27 '17 at 21:23
  • Look for solutions to the JsonMappingExceptions, like https://stackoverflow.com/questions/7625783/jsonmappingexception-no-suitable-constructor-found-for-type-simple-type-class?rq=1, https://stackoverflow.com/questions/12750681/cannot-construct-instance-of-jackson – tkruse Dec 28 '17 at 01:11

1 Answers1

0

I figured it out. I had to add annotations to the constructor arguments, e.g.

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

It worked fine after that.