我正在开发一个Spring Boot应用程序,我想根据JsonView过滤一些端点响应。我正在遵循
中规定的模式https://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring
但是,响应始终包含我的响应对象的完整属性,而不是我期望的子集。
我的(简化)代码:
ftorCar
响应
@RestController
@EnableAutoConfiguration
@ComponentScan
public class Controller {
@RequestMapping("/")
@JsonView(Responder.OnlyMyName.class)
Responder home() throws JsonProcessingException {
Responder responder = new Responder();
return responder
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Controller.class, args);
}
}
响应如下所示:public class Responder {
@JsonView(OnlyMyName.class)
public String name="My name";
@JsonView(Everything.class)
public String value="My value";
public class OnlyMyName{}
public class Everything extends OnlyMyName{}
}
当我希望它为{"name":"My name","value":"My value"}
时。
显然我错过了一些东西,但我无法弄清楚它可能是什么。请帮忙!
答案 0 :(得分:2)
the Jackson parameter MapperFeature.DEFAULT_VIEW_INCLUSION should be set on false.
Something like this
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);