Swagger示例帖子正文 - 如何显示JSON Body-Swagger-annotations

时间:2017-12-31 04:02:06

标签: java spring-boot annotations swagger-ui

要求:我有一个POST方法,它将输入JSON作为String并将其传递给另一个微服务。我不想创建这个输入JSON的Object(Bean)。

方法:

    @ApiOperation(notes = "example" value = "/example", consumes = ".." , method= "..")
    @RequestMapping(name = "xxx" value ="/hello" ..)
    @ApiResponses(..)
        public @ResponseBody String getXXX (@Apiparam(name="JSONrequest", required = true) @RequestBody String JSONrequest){

    }

问题: 生成的Swagger不会将输入显示为JSON模型,其中显示所有JSON属性。

期望: 我想显示我的Swagger这样的东西:

enter image description here

肯定我错过了关键的事情。有什么想法吗?

3 个答案:

答案 0 :(得分:2)

如果从String更改为具体对象不合适(虽然这是我建议你做的,因为它更干净),你可以尝试使用@ApiImplicitParams(查看他们的documentation })

@ApiOperation(notes = "example" value = "/example", consumes = ".." , method= "..")
@ApiImplicitParams({
        @ApiImplicitParam(name = "Object", value = "Object to be created", required = true, dataType = "your.package.BodyClass", paramType = "body")
})
@RequestMapping(name = "xxx" value ="/hello" ..)
@ApiResponses(..)
    public @ResponseBody String getXXX (@Apiparam(name="JSONrequest", required = true) @RequestBody String JSONrequest){

}

(不确定您是否还需要方法参数中的@Apiparam(name="JSONrequest", required = true)位)

答案 1 :(得分:1)

我有类似的问题。我的服务类在String中使用@RequestBody参数。 所以,我做了什么:

创建了一个POJO,并使用@RequestBody注释代替了inputString。

  @RequestMapping(value = "/api/entity/{entityId}/user/query", method = {RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody
    ResponseEntity<String> queryUser(@PathVariable("entityId") String entityId,
                                      @RequestBody QueryUserJsonSchemaPOJO queryUserJsonSchemaPOJO, String inputString,
                                     HttpServletRequest request, HttpServletResponse response)
            throws Exception {
            return userService.queryUserService(inputString, entityId, request);
    }

使用@Around注释创建一个AOP,以更新inputString参数。

    @Around(value = "execution(* com.athmin.rest.UserController.*(..)) || execution(* com.athmin.rest.CityController.*(..)), and args(..) " +
                " && @annotation(com.athmin.annotations.JSONSchemaFileName) ")
        public Object validateRequestBodyAgainstJsonSchema(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    
        Object[] modifiedArgs = proceedingJoinPoint.getArgs();
        
        for (Object o : proceedingJoinPoint.getArgs()) {
                if (o instanceof HttpServletRequest) {
                    HttpServletRequest httpServletRequest = (HttpServletRequest) o;
                    requestBody = httpServletRequest.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
                }
            });
        
for (int i = 0; i < modifiedArgs.length; i++) {
            if (modifiedArgs[i] == null) { // Only inputString is null in my case
                modifiedArgs[i] = requestBody;
            }
        }
        
            proceedingJoinPoint.proceed(modifiedArgs);
    }

答案 2 :(得分:0)

这是一个古老的问题,但是由于我在这里没有找到在线解决方案,因此我如何自定义Java注释自动生成的swagger文档中的示例值。 我使用的是swagger 2.0和springfox.version 2.10.5。

该想法记录了带有@RequestBody批注的请求参数的类。例如我的方法是

    @ApiOperation(
              value = "Start ListBuilder extraction",
              response = ExtractionLogEntity.class,
              produces = "application/json"
            )
    @PostMapping("/extraction/start")
    public ExtractionLogEntity startTask(
        @RequestBody(required = true) ExtractionRequest request,

为了公开请求json对象的示例,我在ExtractionRequest的属性中添加了@ApiModelProperty(example = "...")注释。

   @ApiModelProperty(example = "[{ 'field':'value'}]")
    @NotNull
    private  List<ListBuilderFieldEntity> fields;

   @ApiModelProperty(example = "1000")

    private  String ied;
   @ApiModelProperty(example = "US")
    private  String codebase;

这就是结果

enter image description here