Add support for Jackson serialization views
Spring MVC now supports Jackon's serialization views for rendering different subsets of the same POJO from different controller methods (e.g. detailed page vs summary view). Issue: SPR-7156
This commit is contained in:
committed by
Rossen Stoyanchev
parent
673a497923
commit
be0b69cbf1
@@ -21,6 +21,7 @@ import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -47,6 +48,8 @@ import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -283,6 +286,42 @@ public class RequestResponseBodyMethodProcessorTests {
|
||||
assertTrue("Failed to recognize type-level @RestController", processor.supportsReturnType(returnType));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleResponseBodyJacksonView() throws Exception {
|
||||
Method method = JacksonViewController.class.getMethod("handleResponseBody");
|
||||
HandlerMethod handlerMethod = new HandlerMethod(new JacksonViewController(), method);
|
||||
MethodParameter methodReturnType = handlerMethod.getReturnType();
|
||||
|
||||
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
|
||||
converters.add(new MappingJackson2HttpMessageConverter());
|
||||
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
|
||||
|
||||
processor.handleReturnValue(new JacksonViewController().handleResponseBody(), methodReturnType, mavContainer, webRequest);
|
||||
|
||||
String content = servletResponse.getContentAsString();
|
||||
assertFalse(content.contains("\"withView1\":\"with\""));
|
||||
assertTrue(content.contains("\"withView2\":\"with\""));
|
||||
assertTrue(content.contains("\"withoutView\":\"without\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleResponseBodyJacksonViewAndModelAndView() throws Exception {
|
||||
Method method = JacksonViewController.class.getMethod("handleResponseBodyWithModelAndView");
|
||||
HandlerMethod handlerMethod = new HandlerMethod(new JacksonViewController(), method);
|
||||
MethodParameter methodReturnType = handlerMethod.getReturnType();
|
||||
|
||||
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
|
||||
converters.add(new MappingJackson2HttpMessageConverter());
|
||||
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
|
||||
|
||||
processor.handleReturnValue(new JacksonViewController().handleResponseBody(), methodReturnType, mavContainer, webRequest);
|
||||
|
||||
String content = servletResponse.getContentAsString();
|
||||
assertFalse(content.contains("\"withView1\":\"with\""));
|
||||
assertTrue(content.contains("\"withView2\":\"with\""));
|
||||
assertTrue(content.contains("\"withoutView\":\"without\""));
|
||||
}
|
||||
|
||||
|
||||
public String handle(
|
||||
@RequestBody List<SimpleBean> list,
|
||||
@@ -373,4 +412,69 @@ public class RequestResponseBodyMethodProcessorTests {
|
||||
}
|
||||
}
|
||||
|
||||
private interface MyJacksonView1 {};
|
||||
private interface MyJacksonView2 {};
|
||||
|
||||
private static class JacksonViewBean {
|
||||
|
||||
@JsonView(MyJacksonView1.class)
|
||||
private String withView1;
|
||||
|
||||
@JsonView(MyJacksonView2.class)
|
||||
private String withView2;
|
||||
|
||||
private String withoutView;
|
||||
|
||||
public String getWithView1() {
|
||||
return withView1;
|
||||
}
|
||||
|
||||
public void setWithView1(String withView1) {
|
||||
this.withView1 = withView1;
|
||||
}
|
||||
|
||||
public String getWithView2() {
|
||||
return withView2;
|
||||
}
|
||||
|
||||
public void setWithView2(String withView2) {
|
||||
this.withView2 = withView2;
|
||||
}
|
||||
|
||||
public String getWithoutView() {
|
||||
return withoutView;
|
||||
}
|
||||
|
||||
public void setWithoutView(String withoutView) {
|
||||
this.withoutView = withoutView;
|
||||
}
|
||||
}
|
||||
|
||||
private static class JacksonViewController {
|
||||
|
||||
@RequestMapping
|
||||
@ResponseBody
|
||||
@JsonView(MyJacksonView2.class)
|
||||
public JacksonViewBean handleResponseBody() {
|
||||
JacksonViewBean bean = new JacksonViewBean();
|
||||
bean.setWithView1("with");
|
||||
bean.setWithView2("with");
|
||||
bean.setWithoutView("without");
|
||||
return bean;
|
||||
}
|
||||
|
||||
@RequestMapping
|
||||
@JsonView(MyJacksonView2.class)
|
||||
public ModelAndView handleResponseBodyWithModelAndView() {
|
||||
JacksonViewBean bean = new JacksonViewBean();
|
||||
bean.setWithView1("with");
|
||||
bean.setWithView2("with");
|
||||
bean.setWithoutView("without");
|
||||
ModelAndView mav = new ModelAndView(new MappingJackson2JsonView());
|
||||
mav.addObject("bean", bean);
|
||||
return mav;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -132,12 +132,12 @@ public class ViewResolverTests {
|
||||
View view = vr.resolveViewName("example1", Locale.getDefault());
|
||||
assertEquals("Correct view class", JstlView.class, view.getClass());
|
||||
assertEquals("Correct URL", "example1", ((InternalResourceView) view).getUrl());
|
||||
assertEquals("Correct contentType", "myContentType", ((InternalResourceView) view).getContentType());
|
||||
assertEquals("Correct textContentType", "myContentType", ((InternalResourceView) view).getContentType());
|
||||
|
||||
view = vr.resolveViewName("example2", Locale.getDefault());
|
||||
assertEquals("Correct view class", JstlView.class, view.getClass());
|
||||
assertEquals("Correct URL", "example2", ((InternalResourceView) view).getUrl());
|
||||
assertEquals("Correct contentType", "myContentType", ((InternalResourceView) view).getContentType());
|
||||
assertEquals("Correct textContentType", "myContentType", ((InternalResourceView) view).getContentType());
|
||||
|
||||
HttpServletRequest request = new MockHttpServletRequest(wac.getServletContext());
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mozilla.javascript.Context;
|
||||
@@ -274,6 +275,24 @@ public class MappingJackson2JsonViewTests {
|
||||
assertSame(bean2, ((Map) actual).get("foo2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderSimpleBeanWithJsonView() throws Exception {
|
||||
Object bean = new TestBeanSimple();
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
model.put("bindingResult", mock(BindingResult.class, "binding_result"));
|
||||
model.put("foo", bean);
|
||||
model.put(JsonView.class.getName(), MyJacksonView1.class);
|
||||
|
||||
view.setUpdateContentLength(true);
|
||||
view.render(model, request, response);
|
||||
|
||||
String content = response.getContentAsString();
|
||||
assertTrue(content.length() > 0);
|
||||
assertEquals(content.length(), response.getContentLength());
|
||||
assertTrue(content.contains("foo"));
|
||||
assertFalse(content.contains("42"));
|
||||
}
|
||||
|
||||
private void validateResult() throws Exception {
|
||||
Object jsResult =
|
||||
jsContext.evaluateString(jsScope, "(" + response.getContentAsString() + ")", "JSON Stream", 1, null);
|
||||
@@ -281,14 +300,18 @@ public class MappingJackson2JsonViewTests {
|
||||
assertEquals("application/json", response.getContentType());
|
||||
}
|
||||
|
||||
public interface MyJacksonView1 {};
|
||||
public interface MyJacksonView2 {};
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public static class TestBeanSimple {
|
||||
|
||||
@JsonView(MyJacksonView1.class)
|
||||
private String value = "foo";
|
||||
|
||||
private boolean test = false;
|
||||
|
||||
@JsonView(MyJacksonView2.class)
|
||||
private long number = 42;
|
||||
|
||||
private TestChildBean child = new TestChildBean();
|
||||
|
||||
Reference in New Issue
Block a user