2016-03-03 54 views
0

我正在开发一个Spring Boot应用程序,我想过滤一些基于JsonView的端点响应。我下面的布局模式在JsonView在Spring Boot中没有过滤响应

https://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring

但是响应总是包含我的反应对象的全部特性,而不是我所期望的子集。

我(简化)代码:

@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"}时,我希望它是{"name":"My name"}

显然我错过了一些东西,但我无法弄清楚它可能是什么。请帮忙!

+0

您可能需要'@ ResponseBody'注释 –

+0

'@ ResponseBody'注释是隐含的,当你使用''@RestController – ianaz

回答

2

Jackson参数MapperFeature.DEFAULT_VIEW_INCLUSION应设置为false。

像这样的事情

final ObjectMapper objectMapper = new ObjectMapper(); 
objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false); 
相关问题