问题描述
我知道可以用@JsonView(...)
注释控制器方法以在Spring MVC中静态定义单个视图类.不幸的是,这意味着我可能需要的每种视图都需要一个不同的端点.
I am aware that it is possible to annotate controller methods with @JsonView(...)
to statically define a single view class in Spring MVC. Unfortunately this means that I need a different endpoint for every type of view I might possibly have.
我看到其他人在之前问过.尽管这种方法可能行得通,但Spring通常有很多方法可以完成相同的事情.如果您仅对某些内部知识有一些了解,有时解决方案可能会比最初出现的要简单得多.
I see other people have asked this before. While this approach may work, Spring often has many ways of doing the same thing. Sometimes the solution can be much more simple than it first appears if you just have a bit of knowledge about some of the internals.
我希望有一个控制器端点,该端点可以根据当前主体动态选择适当的视图.我是否可以返回带有属性的Model
,而该属性包含适当的视图类,或者直接包含MappingJacksonValue
实例?
I'd like to have a single controller endpoint that can dynamically select the appropriate view based on the current principal. Is it possible for me to return a Model
with an attribute that contains the appropriate view class or perhaps a MappingJacksonValue
instance directly?
我在org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter#writeInternal
中看到一小段代码,用于确定要使用的视图:
I see in org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter#writeInternal
there is a snippet of code that determines what view to use:
if (value instanceof MappingJacksonValue) {
MappingJacksonValue container = (MappingJacksonValue) object;
value = container.getValue();
serializationView = container.getSerializationView();
}
哪个似乎来自org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice#beforeBodyWriteInternal
,但我无法解决是否有办法仅通过返回包含Jackson2HttpMessageConverter
选择正确视图所需信息的特定值来绕过此方法的问题.
Which appears to come from org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice#beforeBodyWriteInternal
but I'm having trouble working out if there is a way I could bypass this just simply by returning a particular value that contains the necessary information for the Jackson2HttpMessageConverter
to pick the right view.
非常感谢您的帮助.
推荐答案
如果其他人想达成相同的目标,这实际上很简单.
On the off chance someone else wants to achieve the same thing, it actually is very simple.
您可以直接从控制器返回一个org.springframework.http.converter.json.MappingJacksonValue
实例,该实例同时包含要序列化的对象和视图类.
You can directly return aorg.springframework.http.converter.json.MappingJacksonValue
instance from your controller that contains both the object that you want to serialise and the view class.
这将通过org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter#writeInternal
方法拾取,并将使用适当的视图.
This will be picked up by the org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter#writeInternal
method and the appropriate view will be used.
它的工作原理如下:
@RequestMapping(value = "/accounts/{id}", method = GET, produces = APPLICATION_JSON_VALUE)
public MappingJacksonValue getAccount(@PathVariable("id") String accountId, @AuthenticationPrincipal User user) {
final Account account = accountService.get(accountId);
final MappingJacksonValue result = new MappingJacksonValue(account);
final Class<? extends View> view = accountPermissionsService.getViewForUser(user);
result.setSerializationView(view);
return result;
}
这篇关于Spring MVC控制器中JsonView的动态选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!