Spring mvc 如何使用Springfox隐藏或删除Swagger中的额外请求主体参数

Spring mvc 如何使用Springfox隐藏或删除Swagger中的额外请求主体参数,spring-mvc,swagger,swagger-ui,springfox,Spring Mvc,Swagger,Swagger Ui,Springfox,我有一个非常大的用户,有许多字段,如下所示: public class UserDto implements Serializable { private Long id; @Column private String username; @Column private String emailId; @Column private String password; .... many other columns like abo

我有一个非常大的用户,有许多字段,如下所示:

public class UserDto implements Serializable {
    private Long id;
    @Column
    private String username;
    @Column
    private String emailId;
    @Column
    private String password;

    .... many other columns like above with getter/setter
}
    @RequestMapping(value = "authenticate", consumes = "application/json", produces = "application/json", method = RequestMethod.POST)
        @ApiOperation(value="authenticate user",notes="authenticate user for all roles")
        @ApiResponses( {
            @ApiResponse( code = 403, message = "BAD_CREDENCIAL_EXCEPTION",response=ExceptionMsgDto.class),
            @ApiResponse( code = 404, message = "USERNAME_NOT_FOUND_EXCEPTION",response=ExceptionMsgDto.class)
        } )
        public ResponseEntity<Object> authenticate(@RequestBody UserDto userDto, HttpServletRequest request)
                throws Exception {
  /* business logic */
}
我已将身份验证方法定义如下:

public class UserDto implements Serializable {
    private Long id;
    @Column
    private String username;
    @Column
    private String emailId;
    @Column
    private String password;

    .... many other columns like above with getter/setter
}
    @RequestMapping(value = "authenticate", consumes = "application/json", produces = "application/json", method = RequestMethod.POST)
        @ApiOperation(value="authenticate user",notes="authenticate user for all roles")
        @ApiResponses( {
            @ApiResponse( code = 403, message = "BAD_CREDENCIAL_EXCEPTION",response=ExceptionMsgDto.class),
            @ApiResponse( code = 404, message = "USERNAME_NOT_FOUND_EXCEPTION",response=ExceptionMsgDto.class)
        } )
        public ResponseEntity<Object> authenticate(@RequestBody UserDto userDto, HttpServletRequest request)
                throws Exception {
  /* business logic */
}
@RequestMapping(value=“authenticate”,consumes=“application/json”,products=“application/json”,method=RequestMethod.POST)
@ApiOperation(value=“authenticate user”,notes=“authenticate user for all roles”)
@回应({
@ApiResponse(code=403,message=“BAD\u CREDENCIAL\u EXCEPTION”,response=ExceptionMsgDto.class),
@ApiResponse(code=404,message=“USERNAME\u NOT\u FOUND\u EXCEPTION”,response=ExceptionMsgDto.class)
} )
公共响应身份验证(@RequestBody UserDto UserDto,HttpServletRequest)
抛出异常{
/*业务逻辑*/
}
当我生成swagger时,它会在请求模型中显示Userdto的所有属性,但我只想显示用户名/密码,并想隐藏其他属性。但是在createUser方法的同一点上,我想显示UserDto的所有属性

我试图找到解决方案,但没有得到任何结果,这可能吗?请给我一些实现这一目标的方法


提前感谢。

在您的情况下,您应该更好地使用特定于请求的DTO,例如InsertUserDTO、UpdateUserDTO等。显然,除了简单的gette/setter之外,不应该包含任何方法

在我的例子中,由于我不想在我的域对象上添加另一个抽象层(我在Controller方法中传递这些抽象层),所以我只想隐藏特定请求类型的属性,这样它们就不会在Swagger(2.6.1版)上显示

以下是我所做的:

我的域对象:

public class Entity {
   private String name;
   private String hideThis;

   public String getName() { return name; }
   public void setName(String name) { this.name = name; }

   public String getHideThis() { return hideThis; }
   public void setHideThis(String hideThis) { this.hideThis= hideThis; }
}
我注释了所有我想隐藏的属性!即使与私有属性一起工作:)

通过这种方式,我可以控制请求主体在swagger ui中的确切外观。对于插入/更新/删除,它可以不同。当然,这不会阻止用户发送仍将被序列化的内容!api的文档是干净的,没有额外的DTO层,假设您受到保护,不受传递到后端的随机值/对象的影响

以下是我之前的控制器方法:

@RequestMapping(value = "entity", method = RequestMethod.POST)
public Entity storeEntity(@RequestBody final Entity in) {

    return entityService.store(in);
}
以下是我的控制器方法:

@RequestMapping(value = "entity", method = RequestMethod.POST)
public Entity storeEntity(@RequestBody final EntityInsertRequest in) {

   return entityService.store(in);
}