Fabio Ebner
Fabio Ebner

Reputation: 2783

Spring Boot @JsonIgnore

Can I use the JsonIngore in a RestController Method? if I put the @JsonIgnore in my VO he will ignore this prop in all request, but I want to ignore only in some request: sample:

I have

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

Upvotes: 1

Views: 75

Answers (1)

sidgate
sidgate

Reputation: 15254

@JsonView is the right solution for you. Here is an example, and code snippet from the link as below

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

Upvotes: 1

Related Questions