[英]@JsonView declaration for whole Spring MVC rest controler
有什么方法可以為整個Spring MVC rest控制器聲明@JsonView
嗎?
我知道我可以為這樣的特定方法聲明@JsonView
:
@RequestMapping(value = "/user", method = RequestMethod.G
@JsonView(User.WithoutPasswordView.class)
public User getUser() {
return new User("eric", "7!jd#h23");
}
但是我不想為每個方法定義@JsonView
,因為我的方法是在supper類中定義的,並且我不想僅添加單個注釋就覆蓋它們。
我想為整個控制器聲明@JsonView
,就像這樣:
@RestController
@RequestMapping(ZONES_PATH)
@JsonView(User.WithoutPasswordView.class)
public class ZoneResource extends GenericRestService<Zone> {
...
有什么辦法可以做到這一點?
創建將使用預定義視圖的自定義對象映射器
public class CustomObjectMapper extends ObjectMapper {
public CustomObjectMapper() {
super();
setSerializationConfig(getSerializationConfig()
.withView(User.WithoutPasswordView.class));
}
}
將其注冊為spring使用的默認對象映射器
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="jacksonObjectMapper" class="com.web.CustomObjectMapper" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.