Search code examples
javajsonspring-bootswagger

How do I resolve com.fasterxml.jackson.databind.exc.MismatchedInputException?


I'm getting the below error while trying to post a request through Swagger. This works fine when I do it through Postman.

I'm using springBootVersion = '2.1.0.RELEASE', swaggerVersion = '2.9.2'.

This is my Json mapping class. DataExport.java

@XmlRootElement(name = "DataExport")
public class DataExport
{
  @JsonProperty("engineName")
  private String engineName;

  @JsonProperty("searchQuery")
  private String searchQuery;

  public DataExport()
  {
    super();
  }

  public DataExport(String aEngineName, String aSearchQuery)
  {
    super();
    this.engineName = aEngineName;
    this.searchQuery = aSearchQuery;
  }

RestController.java

@RequestMapping(value = "/export", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.TEXT_PLAIN_VALUE)
  @ApiOperation(authorizations = {
      @Authorization(value = "oauth") }, value = "Initiates the job for export", response = String.class)
  @ApiResponses({ @ApiResponse(code = 200, message = "Request accepted."),
      @ApiResponse(code = 500, message = "The export could not be started due to an internal server error.") })
  public String getJobId(@RequestBody DataExport aDataExport, HttpServletResponse aResponse,
      @Value("${com.xyz.dataexportmodule.defaultfilelocation}") final String aLocation)
      throws Exception
  {
    LOG.info("Initializing export for Engine {} and Query {}", aDataExport.getEngineName(),
        aDataExport.getSearchQuery());

    String exportLocation = aLocation
....

I want to pass this JSON { "engineName":"ABC", "searchQuery":"*"

}

But I'm getting this error:

2019-04-01 13:42:05,022 [https-jsse-nio-8443-exec-19] WARN  o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct an instance of `com.xyz.dataexportmodule.persistence.entity.DataExport` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('string'); 
nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct an instance of `com.xyz.dataexportmodule.persistence.entity.DataExport` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('string')
 at [Source: (PushbackInputStream); line: 1, column: 1]]

I'm unable to figure out what is the issue, somebody please help.

P.s: My Swagger Screenshot

enter image description here

Solution: After making these changes in DataExport class and removing Location variable in REST controller method, this issue got resolved.

@JsonIgnoreProperties(ignoreUnknown = true)
@ApiModel(value = "Data Export", description = "A JSON object corresponding to Data Export entity.")
public class DataExport
{
  private String mEngineName;

  private String mSearchQuery;

  /**
   * Default CTor.
   */
  public DataExport()
  {
  }

  @JsonCreator
  public DataExport(@JsonProperty("engineName") String aEngineName,
      @JsonProperty("searchQuery") String aSearchQuery)
  {
    mEngineName = aEngineName;
    mSearchQuery = aSearchQuery;
  }

Solution

  • Your screenshot show that you have two body, which can't be done.

    I don't know what you're trying to do, but just for test remove the field aLocation, and it will be OK.