【问题标题】:Springboot swagger with JsonViewSpringboot 招摇与 JsonView
【发布时间】:2020-07-07 04:26:47
【问题描述】:

可以将swagger 与@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

【问题讨论】:

  • 一些代码可以帮助我们更好地理解您已经完成/正在尝试实现的目标
  • @lealceldeiro 更新:D

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


【解决方案1】:

swagger 可以和@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;
}

【讨论】:

  • 您好,我正在使用 springfox-swagger2 版本 2.9.2(这是 Maven 存储库中的最新版本),但仍然没有考虑 JsonView()
  • @YogeshHShenoy 版本 3.0.0 现在支持 JsonView。
猜你喜欢
  • 2020-02-12
  • 2021-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多