Add JSONP support for MappingJackson2MessageConverter
Issue: SPR-9899
This commit is contained in:
parent
05e96ee448
commit
1338d46a6e
|
|
@ -249,15 +249,27 @@ public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConv
|
||||||
if (this.jsonPrefix != null) {
|
if (this.jsonPrefix != null) {
|
||||||
jsonGenerator.writeRaw(this.jsonPrefix);
|
jsonGenerator.writeRaw(this.jsonPrefix);
|
||||||
}
|
}
|
||||||
|
Class<?> serializationView = null;
|
||||||
|
String jsonpFunction = null;
|
||||||
if (object instanceof MappingJacksonValue) {
|
if (object instanceof MappingJacksonValue) {
|
||||||
MappingJacksonValue valueHolder = (MappingJacksonValue) object;
|
MappingJacksonValue container = (MappingJacksonValue) object;
|
||||||
object = valueHolder.getValue();
|
object = container.getValue();
|
||||||
Class<?> serializationView = valueHolder.getSerializationView();
|
serializationView = container.getSerializationView();
|
||||||
|
jsonpFunction = container.getJsonpFunction();
|
||||||
|
}
|
||||||
|
if (jsonpFunction != null) {
|
||||||
|
jsonGenerator.writeRaw(jsonpFunction + "(" );
|
||||||
|
}
|
||||||
|
if (serializationView != null) {
|
||||||
this.objectMapper.writerWithView(serializationView).writeValue(jsonGenerator, object);
|
this.objectMapper.writerWithView(serializationView).writeValue(jsonGenerator, object);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.objectMapper.writeValue(jsonGenerator, object);
|
this.objectMapper.writeValue(jsonGenerator, object);
|
||||||
}
|
}
|
||||||
|
if (jsonpFunction != null) {
|
||||||
|
jsonGenerator.writeRaw(");");
|
||||||
|
jsonGenerator.flush();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (JsonProcessingException ex) {
|
catch (JsonProcessingException ex) {
|
||||||
throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
|
throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
|
||||||
|
|
|
||||||
|
|
@ -17,44 +17,83 @@
|
||||||
package org.springframework.http.converter.json;
|
package org.springframework.http.converter.json;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds an Object to be serialized via Jackson together with a serialization
|
* A simple holder for the POJO to serialize via
|
||||||
* view to be applied.
|
* {@link org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
|
||||||
|
* MappingJackson2HttpMessageConverter} along with further serialization
|
||||||
|
* instructions to be passed in to the converter.
|
||||||
|
*
|
||||||
|
* <p>On the server side this wrapper is added with a
|
||||||
|
* {@code ResponseBodyInterceptor} after content negotiation selects the
|
||||||
|
* converter to use but before the write.
|
||||||
|
*
|
||||||
|
* <p>On the client side, simply wrap the POJO and pass it in to the
|
||||||
|
* {@code RestTemplate}.
|
||||||
*
|
*
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
* @since 4.1
|
* @since 4.1
|
||||||
*
|
|
||||||
* @see com.fasterxml.jackson.annotation.JsonView
|
|
||||||
*/
|
*/
|
||||||
public class MappingJacksonValue {
|
public class MappingJacksonValue {
|
||||||
|
|
||||||
private final Object value;
|
private Object value;
|
||||||
|
|
||||||
private final Class<?> serializationView;
|
private Class<?> serializationView;
|
||||||
|
|
||||||
|
private String jsonpFunction;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new instance.
|
* Create a new instance wrapping the given POJO to be serialized.
|
||||||
* @param value the Object to be serialized
|
* @param value the Object to be serialized
|
||||||
* @param serializationView the view to be applied
|
|
||||||
*/
|
*/
|
||||||
public MappingJacksonValue(Object value, Class<?> serializationView) {
|
public MappingJacksonValue(Object value) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
this.serializationView = serializationView;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the value to be serialized.
|
* Modify the POJO to serialize.
|
||||||
|
*/
|
||||||
|
public void setValue(Object value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the POJO that needs to be serialized.
|
||||||
*/
|
*/
|
||||||
public Object getValue() {
|
public Object getValue() {
|
||||||
return this.value;
|
return this.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the serialization view to serialize the POJO with.
|
||||||
|
* @see com.fasterxml.jackson.databind.ObjectMapper#writerWithView(Class)
|
||||||
|
* @see com.fasterxml.jackson.annotation.JsonView
|
||||||
|
*/
|
||||||
|
public void setSerializationView(Class<?> serializationView) {
|
||||||
|
this.serializationView = serializationView;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the serialization view to use.
|
* Return the serialization view to use.
|
||||||
|
* @see com.fasterxml.jackson.databind.ObjectMapper#writerWithView(Class)
|
||||||
|
* @see com.fasterxml.jackson.annotation.JsonView
|
||||||
*/
|
*/
|
||||||
public Class<?> getSerializationView() {
|
public Class<?> getSerializationView() {
|
||||||
return this.serializationView;
|
return this.serializationView;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the name of the JSONP function name.
|
||||||
|
*/
|
||||||
|
public void setJsonpFunction(String functionName) {
|
||||||
|
this.jsonpFunction = functionName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the configured JSONP function name.
|
||||||
|
*/
|
||||||
|
public String getJsonpFunction() {
|
||||||
|
return this.jsonpFunction;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ import java.util.Map;
|
||||||
import com.fasterxml.jackson.annotation.JsonView;
|
import com.fasterxml.jackson.annotation.JsonView;
|
||||||
import com.fasterxml.jackson.databind.JavaType;
|
import com.fasterxml.jackson.databind.JavaType;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.hamcrest.CoreMatchers;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.core.ParameterizedTypeReference;
|
import org.springframework.core.ParameterizedTypeReference;
|
||||||
|
|
@ -35,6 +36,7 @@ import org.springframework.http.MockHttpInputMessage;
|
||||||
import org.springframework.http.MockHttpOutputMessage;
|
import org.springframework.http.MockHttpOutputMessage;
|
||||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.*;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -245,13 +247,48 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||||
bean.setWithView1("with");
|
bean.setWithView1("with");
|
||||||
bean.setWithView2("with");
|
bean.setWithView2("with");
|
||||||
bean.setWithoutView("without");
|
bean.setWithoutView("without");
|
||||||
MappingJacksonValue jsv = new MappingJacksonValue(bean, MyJacksonView1.class);
|
|
||||||
this.converter.writeInternal(jsv, outputMessage);
|
MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
|
||||||
|
jacksonValue.setSerializationView(MyJacksonView1.class);
|
||||||
|
this.converter.writeInternal(jacksonValue, outputMessage);
|
||||||
|
|
||||||
String result = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
|
String result = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
|
||||||
assertTrue(result.contains("\"withView1\":\"with\""));
|
assertThat(result, containsString("\"withView1\":\"with\""));
|
||||||
assertFalse(result.contains("\"withView2\":\"with\""));
|
assertThat(result, containsString("\"withoutView\":\"without\""));
|
||||||
assertTrue(result.contains("\"withoutView\":\"without\""));
|
assertThat(result, not(containsString("\"withView2\":\"with\"")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void jsonp() throws Exception {
|
||||||
|
MappingJacksonValue jacksonValue = new MappingJacksonValue("foo");
|
||||||
|
jacksonValue.setSerializationView(MyJacksonView1.class);
|
||||||
|
jacksonValue.setJsonpFunction("callback");
|
||||||
|
|
||||||
|
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||||
|
this.converter.writeInternal(jacksonValue, outputMessage);
|
||||||
|
|
||||||
|
assertEquals("callback(\"foo\");", outputMessage.getBodyAsString(Charset.forName("UTF-8")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void jsonpAndJsonView() throws Exception {
|
||||||
|
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||||
|
JacksonViewBean bean = new JacksonViewBean();
|
||||||
|
bean.setWithView1("with");
|
||||||
|
bean.setWithView2("with");
|
||||||
|
bean.setWithoutView("without");
|
||||||
|
|
||||||
|
MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
|
||||||
|
jacksonValue.setSerializationView(MyJacksonView1.class);
|
||||||
|
jacksonValue.setJsonpFunction("callback");
|
||||||
|
this.converter.writeInternal(jacksonValue, outputMessage);
|
||||||
|
|
||||||
|
String result = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
|
||||||
|
assertThat(result, startsWith("callback("));
|
||||||
|
assertThat(result, endsWith(");"));
|
||||||
|
assertThat(result, containsString("\"withView1\":\"with\""));
|
||||||
|
assertThat(result, containsString("\"withoutView\":\"without\""));
|
||||||
|
assertThat(result, not(containsString("\"withView2\":\"with\"")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -220,8 +220,9 @@ public class RestTemplateIntegrationTests extends AbstractJettyServerTestCase {
|
||||||
HttpHeaders entityHeaders = new HttpHeaders();
|
HttpHeaders entityHeaders = new HttpHeaders();
|
||||||
entityHeaders.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
|
entityHeaders.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
|
||||||
MySampleBean bean = new MySampleBean("with", "with", "without");
|
MySampleBean bean = new MySampleBean("with", "with", "without");
|
||||||
MappingJacksonValue jsv = new MappingJacksonValue(bean, MyJacksonView1.class);
|
MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
|
||||||
HttpEntity<MappingJacksonValue> entity = new HttpEntity<MappingJacksonValue>(jsv);
|
jacksonValue.setSerializationView(MyJacksonView1.class);
|
||||||
|
HttpEntity<MappingJacksonValue> entity = new HttpEntity<MappingJacksonValue>(jacksonValue);
|
||||||
String s = template.postForObject(baseUrl + "/jsonpost", entity, String.class, "post");
|
String s = template.postForObject(baseUrl + "/jsonpost", entity, String.class, "post");
|
||||||
assertTrue(s.contains("\"with1\":\"with\""));
|
assertTrue(s.contains("\"with1\":\"with\""));
|
||||||
assertFalse(s.contains("\"with2\":\"with\""));
|
assertFalse(s.contains("\"with2\":\"with\""));
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,86 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2014 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.web.servlet.mvc.method.annotation;
|
||||||
|
|
||||||
|
import org.springframework.core.MethodParameter;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.converter.json.MappingJacksonValue;
|
||||||
|
import org.springframework.http.server.ServerHttpRequest;
|
||||||
|
import org.springframework.http.server.ServerHttpResponse;
|
||||||
|
import org.springframework.http.server.ServletServerHttpRequest;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A convenient base class for a {@code ResponseBodyInterceptor} to instruct the
|
||||||
|
* {@link org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
|
||||||
|
* MappingJackson2HttpMessageConverter} to serialize with JSONP formatting.
|
||||||
|
*
|
||||||
|
* <p>Sub-classes must specify the query parameter name(s) to check for the name
|
||||||
|
* of the JSONP callback function.
|
||||||
|
*
|
||||||
|
* <p>Sub-classes are likely to be annotated with the {@code @ControllerAdvice}
|
||||||
|
* annotation and auto-detected or otherwise must be registered directly with the
|
||||||
|
* {@code RequestMappingHandlerAdapter} and {@code ExceptionHandlerExceptionResolver}.
|
||||||
|
*
|
||||||
|
* @author Rossen Stoyanchev
|
||||||
|
* @since 4.1
|
||||||
|
*/
|
||||||
|
public abstract class AbstractJsonpResponseBodyInterceptor extends AbstractMappingJacksonResponseBodyInterceptor {
|
||||||
|
|
||||||
|
private final String[] jsonpQueryParamNames;
|
||||||
|
|
||||||
|
|
||||||
|
protected AbstractJsonpResponseBodyInterceptor(Collection<String> queryParamNames) {
|
||||||
|
Assert.isTrue(!CollectionUtils.isEmpty(queryParamNames), "At least one query param name is required");
|
||||||
|
this.jsonpQueryParamNames = queryParamNames.toArray(new String[queryParamNames.size()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType,
|
||||||
|
MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {
|
||||||
|
|
||||||
|
HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
|
||||||
|
|
||||||
|
for (String name : this.jsonpQueryParamNames) {
|
||||||
|
String value = servletRequest.getParameter(name);
|
||||||
|
if (value != null) {
|
||||||
|
MediaType contentTypeToUse = getContentType(contentType, request, response);
|
||||||
|
response.getHeaders().setContentType(contentTypeToUse);
|
||||||
|
bodyContainer.setJsonpFunction(value);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the content type to set the response to.
|
||||||
|
* This implementation always returns "application/javascript".
|
||||||
|
* @param contentType the content type selected through content negotiation
|
||||||
|
* @param request the current request
|
||||||
|
* @param response the current response
|
||||||
|
* @return the content type to set the response to
|
||||||
|
*/
|
||||||
|
protected MediaType getContentType(MediaType contentType, ServerHttpRequest request, ServerHttpResponse response) {
|
||||||
|
return new MediaType("application", "javascript");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,72 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2014 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.web.servlet.mvc.method.annotation;
|
||||||
|
|
||||||
|
import org.springframework.core.MethodParameter;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.converter.HttpMessageConverter;
|
||||||
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||||
|
import org.springframework.http.converter.json.MappingJacksonValue;
|
||||||
|
import org.springframework.http.server.ServerHttpRequest;
|
||||||
|
import org.springframework.http.server.ServerHttpResponse;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A convenient base class for {@code ResponseBodyInterceptor} implementations
|
||||||
|
* that customize the response before JSON serialization with
|
||||||
|
* {@link org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
|
||||||
|
* MappingJackson2HttpMessageConverter}.
|
||||||
|
*
|
||||||
|
* @author Rossen Stoyanchev
|
||||||
|
* @since 4.1
|
||||||
|
*/
|
||||||
|
public abstract class AbstractMappingJacksonResponseBodyInterceptor implements ResponseBodyInterceptor {
|
||||||
|
|
||||||
|
|
||||||
|
protected AbstractMappingJacksonResponseBodyInterceptor() {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
public final <T> T beforeBodyWrite(T body, MediaType contentType,
|
||||||
|
Class<? extends HttpMessageConverter<T>> converterType,
|
||||||
|
MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {
|
||||||
|
|
||||||
|
if (!MappingJackson2HttpMessageConverter.class.equals(converterType)) {
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
MappingJacksonValue container = getOrCreateContainer(body);
|
||||||
|
beforeBodyWriteInternal(container, contentType, returnType, request, response);
|
||||||
|
return (T) container;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrap the body in a {@link MappingJacksonValue} value container (for providing
|
||||||
|
* additional serialization instructions) or simply cast it if already wrapped.
|
||||||
|
*/
|
||||||
|
protected MappingJacksonValue getOrCreateContainer(Object body) {
|
||||||
|
return (body instanceof MappingJacksonValue) ? (MappingJacksonValue) body : new MappingJacksonValue(body);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoked only if the converter type is {@code MappingJackson2HttpMessageConverter}.
|
||||||
|
*/
|
||||||
|
protected abstract void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType,
|
||||||
|
MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -19,42 +19,40 @@ package org.springframework.web.servlet.mvc.method.annotation;
|
||||||
import com.fasterxml.jackson.annotation.JsonView;
|
import com.fasterxml.jackson.annotation.JsonView;
|
||||||
import org.springframework.core.MethodParameter;
|
import org.springframework.core.MethodParameter;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.converter.HttpMessageConverter;
|
|
||||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
|
||||||
import org.springframework.http.converter.json.MappingJacksonValue;
|
import org.springframework.http.converter.json.MappingJacksonValue;
|
||||||
import org.springframework.http.server.ServerHttpRequest;
|
import org.springframework.http.server.ServerHttpRequest;
|
||||||
import org.springframework.http.server.ServerHttpResponse;
|
import org.springframework.http.server.ServerHttpResponse;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@code ResponseBodyInterceptor} implementation that adds support for the
|
* A {@code ResponseBodyInterceptor} implementation that adds support for
|
||||||
* Jackson {@code @JsonView} annotation on a Spring MVC {@code @RequestMapping}
|
* Jackson's {@code @JsonView} annotation declared on a Spring MVC
|
||||||
* or {@code @ExceptionHandler} method.
|
* {@code @RequestMapping} or {@code @ExceptionHandler} method. The serialization
|
||||||
|
* view specified in the annotation will be passed in to the
|
||||||
|
* {@code MappingJackson2HttpMessageConverter} which will then use it to
|
||||||
|
* serialize the response body with.
|
||||||
*
|
*
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
* @since 4.1
|
* @since 4.1
|
||||||
|
*
|
||||||
|
* @see com.fasterxml.jackson.databind.ObjectMapper#writerWithView(Class)
|
||||||
*/
|
*/
|
||||||
public class JsonViewResponseBodyInterceptor implements ResponseBodyInterceptor {
|
public class JsonViewResponseBodyInterceptor extends AbstractMappingJacksonResponseBodyInterceptor {
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType,
|
||||||
public <T> T beforeBodyWrite(T body, MediaType contentType, Class<? extends HttpMessageConverter<T>> converterType,
|
|
||||||
MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {
|
MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {
|
||||||
|
|
||||||
if (!MappingJackson2HttpMessageConverter.class.equals(converterType)) {
|
|
||||||
return body;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonView annotation = returnType.getMethodAnnotation(JsonView.class);
|
JsonView annotation = returnType.getMethodAnnotation(JsonView.class);
|
||||||
if (annotation == null) {
|
if (annotation == null) {
|
||||||
return body;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Assert.isTrue(annotation.value().length != 0,
|
Assert.isTrue(annotation.value().length != 0,
|
||||||
"Expected at least one serialization view class in JsonView annotation on " + returnType);
|
"Expected at least one serialization view class in JsonView annotation on " + returnType);
|
||||||
|
|
||||||
return (T) new MappingJacksonValue(body, annotation.value()[0]);
|
bodyContainer.setSerializationView(annotation.value()[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,12 @@
|
||||||
package org.springframework.web.servlet.mvc.method.annotation;
|
package org.springframework.web.servlet.mvc.method.annotation;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
|
|
@ -28,6 +33,8 @@ import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.http.converter.HttpMessageConverter;
|
import org.springframework.http.converter.HttpMessageConverter;
|
||||||
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||||
|
import org.springframework.http.converter.json.MappingJacksonValue;
|
||||||
import org.springframework.http.server.ServerHttpRequest;
|
import org.springframework.http.server.ServerHttpRequest;
|
||||||
import org.springframework.http.server.ServerHttpResponse;
|
import org.springframework.http.server.ServerHttpResponse;
|
||||||
import org.springframework.http.server.ServletServerHttpResponse;
|
import org.springframework.http.server.ServletServerHttpResponse;
|
||||||
|
|
@ -229,15 +236,23 @@ public class RequestMappingHandlerAdapterTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void responseBodyInterceptor() throws Exception {
|
public void responseBodyInterceptor() throws Exception {
|
||||||
|
List<HttpMessageConverter<?>> converters = new ArrayList<>();
|
||||||
|
converters.add(new MappingJackson2HttpMessageConverter());
|
||||||
|
this.handlerAdapter.setMessageConverters(converters);
|
||||||
|
|
||||||
this.webAppContext.registerSingleton("rba", ResponseCodeSuppressingAdvice.class);
|
this.webAppContext.registerSingleton("rba", ResponseCodeSuppressingAdvice.class);
|
||||||
|
this.webAppContext.registerSingleton("ja", JsonpAdvice.class);
|
||||||
this.webAppContext.refresh();
|
this.webAppContext.refresh();
|
||||||
|
|
||||||
|
this.request.addHeader("Accept", MediaType.APPLICATION_JSON_VALUE);
|
||||||
|
this.request.setParameter("c", "callback");
|
||||||
|
|
||||||
HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handleWithResponseEntity");
|
HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handleWithResponseEntity");
|
||||||
this.handlerAdapter.afterPropertiesSet();
|
this.handlerAdapter.afterPropertiesSet();
|
||||||
this.handlerAdapter.handle(this.request, this.response, handlerMethod);
|
this.handlerAdapter.handle(this.request, this.response, handlerMethod);
|
||||||
|
|
||||||
assertEquals(200, this.response.getStatus());
|
assertEquals(200, this.response.getStatus());
|
||||||
assertEquals("status=400, message=body", this.response.getContentAsString());
|
assertEquals("callback({\"status\":400,\"message\":\"body\"});", this.response.getContentAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -324,17 +339,28 @@ public class RequestMappingHandlerAdapterTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ControllerAdvice
|
@ControllerAdvice
|
||||||
private static class ResponseCodeSuppressingAdvice implements ResponseBodyInterceptor {
|
private static class ResponseCodeSuppressingAdvice extends AbstractMappingJacksonResponseBodyInterceptor {
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
public <T> T beforeBodyWrite(T body, MediaType contentType,
|
protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType,
|
||||||
Class<? extends HttpMessageConverter<T>> converterType,
|
|
||||||
MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {
|
MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {
|
||||||
|
|
||||||
int status = ((ServletServerHttpResponse) response).getServletResponse().getStatus();
|
int status = ((ServletServerHttpResponse) response).getServletResponse().getStatus();
|
||||||
response.setStatusCode(HttpStatus.OK);
|
response.setStatusCode(HttpStatus.OK);
|
||||||
return (T) ("status=" + status + ", message=" + body);
|
|
||||||
|
Map<String, Object> map = new LinkedHashMap<>();
|
||||||
|
map.put("status", status);
|
||||||
|
map.put("message", bodyContainer.getValue());
|
||||||
|
bodyContainer.setValue(map);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ControllerAdvice
|
||||||
|
private static class JsonpAdvice extends AbstractJsonpResponseBodyInterceptor {
|
||||||
|
|
||||||
|
public JsonpAdvice() {
|
||||||
|
super(Arrays.asList("c"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue