我如何使用 JSONDOC 将 headers 指定为 application/json

How can i specify headers as application/json using JSONDOC

我正在使用 Spring4 来设计我的 REST API。但是当我使用 JsonDoc 记录它时,它给我一个错误:

The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.

Apache Tomcat/7.0.62

", "status": 415, "statusText": "Unsupported Media Type" }

@ApiMethod
@ApiHeaders(headers={ @ApiHeader(name="Content-Type", allowedvalues="application/json",description="application/json")})
@RequestMapping(value="/test" ,method=RequestMethod.POST)   
public @ApiResponseObject  @ResponseBody ResponseMessage test(@ApiBodyObject @RequestBody TestDto test){
    System.out.println(test.getId());
    return testService.addTestMessage("hello demo");
}

如何测试?

我认为这与 JSONDoc 无关。您需要检查您是否发送有效的 Content-Type,如 application/json。

另请检查 TestDto JSON 表示是否正确转换为 JAVA 对象。如果您的 JSON 字符串无法转换 java 对象,Spring 会抛出此错误。

If you are using Jackson API, You can use below sample to get exception and see cause.

    String myJsonString="Your JSON String";
    ObjectMapper mapper = new ObjectMapper();
    try {
        mapper.readValue(myJsonString,TestDto.class);
    } catch (JsonParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JsonMappingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

检查您的内容类型并检查您的负载格式。

可能有一些属性不匹配。