我可以解决com.fasterxml.jackson.databind.exc.MismatchedInputException吗?

问题描述 投票:1回答:1

我试图通过Swagger发布请求时遇到错误。当我通过邮差时,这很好用。

我正在使用springBootVersion ='2.1.0.RELEASE',swaggerVersion ='2.9.2'。

这是我的Json映射类。 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;
  }

rest controller.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
....

我想通过这个json {“engineName”:“ABC”,“searchQuery”:“*”

}

但是我收到了这个错误:

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 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 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]]

我无法弄清问题是什么,有人请帮忙。

P.s:My Swagger截图

enter image description here

解决方案:在DataExport类中进行这些更改并在REST控制器方法中删除Location变量后,此问题得到了解决。

@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;
  }
java json spring-boot swagger
1个回答
1
投票

你的截图显示你有两个身体,不能是done

我不知道你要做什么,但只是为了测试删除字段aLocation,它会没问题。

© www.soinside.com 2019 - 2024. All rights reserved.