使用JsonView的Springboot招摇

时间:2018-10-19 15:38:31

标签: spring-boot swagger-ui json-view

是否可以将招摇与@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

1 个答案:

答案 0 :(得分:0)

  

是否可以将招摇与@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;
}