尝试通过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;
}
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
....
我想传递这个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:我的招摇截图
解决方案:在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;
}