Spring boot springboot@JsonIgnore
我可以在RestController方法中使用JsonIngore吗?如果我把@JsonIgnore放在我的VO中,他会在所有请求中忽略这个道具,但我只想在某些请求中忽略它:示例: 我有Spring boot springboot@JsonIgnore,spring-boot,Spring Boot,我可以在RestController方法中使用JsonIngore吗?如果我把@JsonIgnore放在我的VO中,他会在所有请求中忽略这个道具,但我只想在某些请求中忽略它:示例: 我有 public class Pedido{ private Long id; private Date day; private List<Item> items; } public class Item { private Long id; private Str
public class Pedido{
private Long id;
private Date day;
private List<Item> items;
}
public class Item {
private Long id;
private String nome;
}
@RestController
@RequestMapping("/pedido")
public class PedidoController{
@GetMapping(value = "/")
public List<Pedido> findAll(){
//HERE I dont need to return the List<Item>
}
@GetMapping(value = "/{id}")
public Pedido findById(@PathVariable Long id){
//HERE I need to return the List<Item>
}
}
public-class-Pedido{
私人长id;
私人约会日;
私人清单项目;
}
公共类项目{
私人长id;
私有字符串名称;
}
@RestController
@请求映射(“/pedido”)
公共级PedidoController{
@GetMapping(value=“/”)
公共列表findAll(){
//在这里,我不需要返回列表
}
@GetMapping(value=“/{id}”)
公共Pedido findById(@PathVariable Long id){
//在这里,我需要返回列表
}
}
@JsonView
是适合您的解决方案。是一个示例,下面是链接中的代码片段
public class View {
interface Summary {}
}
public class User {
@JsonView(View.Summary.class)
private Long id;
@JsonView(View.Summary.class)
private String firstname;
@JsonView(View.Summary.class)
private String lastname;
private String email;
private String address;
}
@RestController
public class MessageController {
@Autowired
private MessageService messageService;
@JsonView(View.Summary.class)
@RequestMapping("/")
public List<Message> getAllMessages() {
return messageService.getAll();
}
@RequestMapping("/{id}")
public Message getMessage(@PathVariable Long id) {
return messageService.get(id);
}
}
公共类视图{
接口摘要{}
}
公共类用户{
@JsonView(View.Summary.class)
私人长id;
@JsonView(View.Summary.class)
私有字符串名;
@JsonView(View.Summary.class)
私有字符串lastname;
私人字符串电子邮件;
私有字符串地址;
}
@RestController
公共类消息控制器{
@自动连线
私人信息服务;
@JsonView(View.Summary.class)
@请求映射(“/”)
公共列表getAllMessages(){
returnmessageservice.getAll();
}
@请求映射(“/{id}”)
公共消息getMessage(@PathVariable Long id){
returnmessageservice.get(id);
}
}