Springboot 大摇大摆与 JsonView [英] Springboot swagger with JsonView

查看:53
本文介绍了Springboot 大摇大摆与 JsonView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是我的模型:

公共类 Intimacao 扩展 EntityBase {@嵌入式@JsonView({View.Intimacao_Lista.class})私人 Devedor 开发商;@嵌入式私人萨卡多萨卡多;@嵌入式私人 Apresentante apresentante;@嵌入式私人名称;}

这是我的控制器:

@GetMapping("/")@PreAuthorize("hasRole('ADMINISTRADOR') 或 hasRole('MOTOBOY')")@JsonView({View.Intimacao_Lista.class})公开名单<Intimacao>listar(校长校长){System.out.println(principal.getName());返回空;}

这是swagger-ui

的结果<预><代码>[{出席者":{"documento": "字符串",名称":字符串"},开发者":{"bairro": "字符串","cep": "字符串","cidade": "字符串","complemento": "字符串","documento": "字符串","estado": "字符串","logradouro": "字符串","名称": "字符串","数字": "字符串","tipoLogradorouo": "字符串"},身份证":0,萨卡多尔":{"chave": "字符串","documento": "字符串","especie": "字符串",名称":字符串"},标题":{custas1":0,custas2":0,custas3":0,custas4":0,custas5":0,custas6":0,custas7":0,custas8":0,custas9":0,"数字": "字符串","vencimento": "字符串"}}]

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

解决方案

可以将 swagger 与 @JsonView 集成吗?

(部分).

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

响应对象(这部分工作正常).

@GetMapping("/")@PreAuthorize("hasRole('ADMINISTRADOR') 或 hasRole('MOTOBOY')")@JsonView({View.Intimacao_Lista.class})公开名单<Intimacao>listar(校长校长){System.out.println(principal.getName());返回空;}

RequestBody 对象(还没有,正在拉取请求,#2918评论 #2079).在你的情况下:

@GetMapping("/")@PreAuthorize("hasRole('ADMINISTRADOR') 或 hasRole('MOTOBOY')")@JsonView({View.Intimacao_Lista.class})//将`Views.Principal.class` 替换为合适的值公开名单<Intimacao>listar(@JsonView(Views.Principal.class) 委托人){System.out.println(principal.getName());返回空;}

It's possible to integrate the swagger with @JsonView? I have one model that I use the @JsonView to return only a few fields, but the swagger-ui shows the hole model.

This is my model:

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;

}

This is my controller:

@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;
}

This is the result in 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"
    }
  }
]

But if I GET my API only will return the devedor properties, because the @JsonView

解决方案

It's possible to integrate the swagger with @JsonView?

Yes (partially).

After this pull request was merged you can use it for:

Response Objects (you got that part working).

@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 Objects (not yet, pull request on its way, see #2918 and an example in comment at #2079). In your case:

@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;
}

这篇关于Springboot 大摇大摆与 JsonView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆