Spring MVC忽略给定控制器方法的Json属性

时间:2021-08-19 14:42:00

I have a Java class (MyResponse) that is returned by a multiple RestController methods and has a lot of fields.

我有一个Java类(MyResponse),它由多个RestController方法返回,并且有很多字段。

@RequestMapping(value = "offering", method=RequestMethod.POST)
public ResponseEntity<MyResponse> postOffering(...) {}

@RequestMapping(value = "someOtherMethod", method=RequestMethod.POST)
public ResponseEntity<MyResponse> someOtherMethod(...) {}

I want to ignore (e.g. not serialize it) one of the properties for just one method.

我想忽略(例如,不序列化)一个方法的属性之一。

I don't want to ignore null fields for the class, because it may have a side effect on other fields.

我不想忽略该类的空字段,因为它可能对其他字段有副作用。

@JsonInclude(Include.NON_NULL)
public class MyResponse { ... }

The JsonView looks good, but as far as I understand I have to annotate all other fields in the class with a @JsonView except the one that I want to ignore which sounds clumsy. If there is a way to do something like "reverse JsonView" it will be great.

JsonView看起来不错,但据我所知,我必须使用@JsonView注释类中的所有其他字段,除了我想忽略的那些听起来很笨拙的字段。如果有办法做“反向JsonView”这样的话会很棒。

Any ideas on how to ignore a property for a controller method?

关于如何忽略控制器方法的属性的任何想法?

1 个解决方案

#1


3  

Props to this guy.

道具给这个家伙。

By default (and in Spring Boot) MapperFeature.DEFAULT_VIEW_INCLUSION is enabled in Jackson. That means that all fields are included by default.

默认情况下(在Spring Boot中)在Jackson中启用了MapperFeature.DEFAULT_VIEW_INCLUSION。这意味着默认包含所有字段。

But if you annotate any field with a view that is different than the one on the controller method this field will be ignored.

但是,如果您使用与控制器方法上的视图不同的视图注释任何字段,则此字段将被忽略。

public class View {
    public interface Default{}
    public interface Ignore{}
}

@JsonView(View.Default.class) //this method will ignore fields that are not annotated with View.Default
@RequestMapping(value = "offering", method=RequestMethod.POST)
public ResponseEntity<MyResponse> postOffering(...) {}

//this method will serialize all fields
@RequestMapping(value = "someOtherMethod", method=RequestMethod.POST)
public ResponseEntity<MyResponse> someOtherMethod(...) {}

public class MyResponse { 
    @JsonView(View.Ignore.class)
    private String filed1;
    private String field2;
}

#1


3  

Props to this guy.

道具给这个家伙。

By default (and in Spring Boot) MapperFeature.DEFAULT_VIEW_INCLUSION is enabled in Jackson. That means that all fields are included by default.

默认情况下(在Spring Boot中)在Jackson中启用了MapperFeature.DEFAULT_VIEW_INCLUSION。这意味着默认包含所有字段。

But if you annotate any field with a view that is different than the one on the controller method this field will be ignored.

但是,如果您使用与控制器方法上的视图不同的视图注释任何字段,则此字段将被忽略。

public class View {
    public interface Default{}
    public interface Ignore{}
}

@JsonView(View.Default.class) //this method will ignore fields that are not annotated with View.Default
@RequestMapping(value = "offering", method=RequestMethod.POST)
public ResponseEntity<MyResponse> postOffering(...) {}

//this method will serialize all fields
@RequestMapping(value = "someOtherMethod", method=RequestMethod.POST)
public ResponseEntity<MyResponse> someOtherMethod(...) {}

public class MyResponse { 
    @JsonView(View.Ignore.class)
    private String filed1;
    private String field2;
}