首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Swagger示例post Body-如何显示JSON body - Swagger-annotations

Swagger示例post Body-如何显示JSON body - Swagger-annotations
EN

Stack Overflow用户
提问于 2017-12-31 12:02:06
回答 3查看 22K关注 0票数 7

要求:我有一个POST方法,它接受输入JSON作为字符串,并将其传递给另一个微服务。我不想创建此输入JSON的对象(Bean)。

方法:

代码语言:javascript
复制
    @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,如下所示:

毫无疑问,我遗漏了关键的东西。有什么想法吗?

EN

回答 3

Stack Overflow用户

发布于 2017-12-31 17:34:17

如果不能从String更改为具体的对象(尽管这是我建议您做的,因为它更简洁),您可以尝试使用@ApiImplicitParams (查看他们的documentation)

代码语言:javascript
复制
@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)位)

票数 3
EN

Stack Overflow用户

发布于 2020-09-29 22:46:08

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

其思想是记录具有@RequestBody注释的请求参数的类。例如,我的方法是

代码语言:javascript
复制
    @ApiOperation(
              value = "Start ListBuilder extraction",
              response = ExtractionLogEntity.class,
              produces = "application/json"
            )
    @PostMapping("/extraction/start")
    public ExtractionLogEntity startTask(
        @RequestBody(required = true) ExtractionRequest request,

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

代码语言:javascript
复制
   @ApiModelProperty(example = "[{ 'field':'value'}]")
    @NotNull
    private  List<ListBuilderFieldEntity> fields;

   @ApiModelProperty(example = "1000")

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

这就是结果

票数 2
EN

Stack Overflow用户

发布于 2020-09-26 12:36:44

我也遇到过类似的问题。我的服务类在字符串中接受@RequestBody参数。所以,我所做的是:

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

代码语言:javascript
复制
  @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参数。

代码语言:javascript
复制
    @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);
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48039876

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档