可以将招摇与@JsonView集成吗?我有一个模型,使用@JsonView仅返回几个字段,但是swagger-ui显示了孔模型。

这是我的模型:

public class Intimacao extends EntityBase {
    @Embedded
    @JsonView({View.Intimacao_Lista.class})
    private Devedor devedor;
    @Embedded
    private Sacador sacador;
    @Embedded
    private Apresentante apresentante;
    @Embedded
    private Titulo titulo;

}


这是我的控制器:

@GetMapping("/")
@PreAuthorize("hasRole('ADMINISTRADOR') or hasRole('MOTOBOY')")
@JsonView({View.Intimacao_Lista.class})
public List<Intimacao> listar(Principal principal){
    System.out.println(principal.getName());
    return null;
}


这是swagger-ui的结果

[
  {
    "apresentante": {
      "documento": "string",
      "nome": "string"
    },
    "devedor": {
      "bairro": "string",
      "cep": "string",
      "cidade": "string",
      "complemento": "string",
      "documento": "string",
      "estado": "string",
      "logradouro": "string",
      "nome": "string",
      "numero": "string",
      "tipoLogradorouo": "string"
    },
    "id": 0,
    "sacador": {
      "chave": "string",
      "documento": "string",
      "especie": "string",
      "nome": "string"
    },
    "titulo": {
      "custas1": 0,
      "custas2": 0,
      "custas3": 0,
      "custas4": 0,
      "custas5": 0,
      "custas6": 0,
      "custas7": 0,
      "custas8": 0,
      "custas9": 0,
      "numero": "string",
      "vencimento": "string"
    }
  }
]


但是,如果我GET我的API仅会返回devedor属性,因为@JsonView

最佳答案

可以将招摇与@JsonView集成吗?


是的(部分)。

合并此pull request后,您可以将其用于:

响应对象(使该部分起作用)。

@GetMapping("/")
@PreAuthorize("hasRole('ADMINISTRADOR') or hasRole('MOTOBOY')")
@JsonView({View.Intimacao_Lista.class})
public List<Intimacao> listar(Principal principal){
    System.out.println(principal.getName());
    return null;
}


RequestBody对象(尚未完成,请拉请求,请参见#2918comment at #2079中的示例)。在您的情况下:

@GetMapping("/")
@PreAuthorize("hasRole('ADMINISTRADOR') or hasRole('MOTOBOY')")
@JsonView({View.Intimacao_Lista.class})
// replace `Views.Principal.class` for the proper value
public List<Intimacao> listar(@JsonView(Views.Principal.class) Principal principal){
    System.out.println(principal.getName());
    return null;
}

09-26 20:42