带有 JsonView 的 Springboot 大摇大摆
Springboot swagger with JsonView
可以将 swagger 与 @JsonView
集成吗?我有一个模型,我只在几个字段中使用 @JsonView
到 return,但是 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 只会 return devedor
属性,因为 @JsonView
It's possible to integrate the swagger with @JsonView
?
是(部分)。
合并 pull request 后,您可以将其用于:
Response Objects(你让那部分工作了)。
@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 对象(还没有,拉取请求正在路上, 见 #2918 and an example in comment 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;
}
可以将 swagger 与 @JsonView
集成吗?我有一个模型,我只在几个字段中使用 @JsonView
到 return,但是 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 只会 return devedor
属性,因为 @JsonView
It's possible to integrate the swagger with
@JsonView
?
是(部分)。
合并 pull request 后,您可以将其用于:
Response Objects(你让那部分工作了)。
@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 对象(还没有,拉取请求正在路上, 见 #2918 and an example in comment 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;
}