1

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;
  }
VVD
  • 178
  • 1
  • 9
Daffy
  • 140
  • 2
  • 13
  • From the stack trace, it is saying "no String-argument constructor" so the post body is being interpreted as string and not as Json. – Abhijith Nagarajan Apr 01 '19 at 12:26
  • How do I fix it ? I tried @jsoncreator annotator before the constructor public DataExport(...), still no luck. – Daffy Apr 01 '19 at 12:37
  • Can you please show how you're sending the json on swagger? – Madhu Bhat Apr 01 '19 at 13:09
  • @MadhuBhat I have attached the screenshot at the end of my post. – Daffy Apr 01 '19 at 13:30
  • Try to check the curl command from your swagger call. Using Chrome, open the developers console in your browser, and check the network tab, you will be able to export the http call to a curl command. Then you can compare the curl command with the postman call. If postman works, then your problem is not in your code. Or rather, maybe is in your code but around the swagger annotations – Perimosh Apr 01 '19 at 13:37
  • Remove the `"string"` value for aLocation on the swagger ui and try once. – Madhu Bhat Apr 01 '19 at 13:42

1 Answers1

1

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.

lmoal
  • 106
  • 5
  • Thank you so much. This worked. I was passing my default location through my configuration.xml directly through my controller. After removing it and making some changes to DataExport.java it worked, – Daffy Apr 01 '19 at 13:45