Search in sources :

Example 66 with HttpMessageConverter

use of org.springframework.http.converter.HttpMessageConverter in project spring-framework by spring-projects.

the class RequestResponseBodyMethodProcessorTests method defaultCharset.

// SPR-13631
@Test
public void defaultCharset() throws Exception {
    Method method = JacksonController.class.getMethod("defaultCharset");
    HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
    MethodParameter methodReturnType = handlerMethod.getReturnType();
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new MappingJackson2HttpMessageConverter());
    RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
    Object returnValue = new JacksonController().defaultCharset();
    processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request);
    assertEquals("UTF-8", this.servletResponse.getCharacterEncoding());
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 67 with HttpMessageConverter

use of org.springframework.http.converter.HttpMessageConverter in project spring-framework by spring-projects.

the class RequestResponseBodyMethodProcessorTests method jacksonJsonViewWithResponseEntityAndJsonMessageConverter.

@Test
public void jacksonJsonViewWithResponseEntityAndJsonMessageConverter() throws Exception {
    Method method = JacksonController.class.getMethod("handleResponseEntity");
    HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
    MethodParameter methodReturnType = handlerMethod.getReturnType();
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new MappingJackson2HttpMessageConverter());
    HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters, null, Collections.singletonList(new JsonViewResponseBodyAdvice()));
    Object returnValue = new JacksonController().handleResponseEntity();
    processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request);
    String content = this.servletResponse.getContentAsString();
    assertFalse(content.contains("\"withView1\":\"with\""));
    assertTrue(content.contains("\"withView2\":\"with\""));
    assertFalse(content.contains("\"withoutView\":\"without\""));
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 68 with HttpMessageConverter

use of org.springframework.http.converter.HttpMessageConverter in project spring-framework by spring-projects.

the class RequestMappingHandlerAdapterTests method jsonpResponseBodyAdvice.

@Test
public void jsonpResponseBodyAdvice() throws Exception {
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new MappingJackson2HttpMessageConverter());
    this.handlerAdapter.setMessageConverters(converters);
    this.webAppContext.registerSingleton("jsonpAdvice", JsonpAdvice.class);
    this.webAppContext.refresh();
    testJsonp("callback", true);
    testJsonp("_callback", true);
    testJsonp("_Call.bAcK", true);
    testJsonp("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_.", true);
    testJsonp("<script>", false);
    testJsonp("!foo!bar", false);
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) Test(org.junit.Test)

Example 69 with HttpMessageConverter

use of org.springframework.http.converter.HttpMessageConverter in project spring-framework by spring-projects.

the class RequestPartMethodArgumentResolverTests method setup.

@Before
@SuppressWarnings("unchecked")
public void setup() throws Exception {
    messageConverter = mock(HttpMessageConverter.class);
    given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
    resolver = new RequestPartMethodArgumentResolver(Collections.<HttpMessageConverter<?>>singletonList(messageConverter));
    reset(messageConverter);
    byte[] content = "doesn't matter as long as not empty".getBytes(StandardCharsets.UTF_8);
    multipartFile1 = new MockMultipartFile("requestPart", "", "text/plain", content);
    multipartFile2 = new MockMultipartFile("requestPart", "", "text/plain", content);
    multipartRequest = new MockMultipartHttpServletRequest();
    multipartRequest.addFile(multipartFile1);
    multipartRequest.addFile(multipartFile2);
    multipartRequest.addFile(new MockMultipartFile("otherPart", "", "text/plain", content));
    webRequest = new ServletWebRequest(multipartRequest, new MockHttpServletResponse());
    Method method = ReflectionUtils.findMethod(getClass(), "handle", (Class<?>[]) null);
    paramRequestPart = new SynthesizingMethodParameter(method, 0);
    paramRequestPart.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
    paramNamedRequestPart = new SynthesizingMethodParameter(method, 1);
    paramValidRequestPart = new SynthesizingMethodParameter(method, 2);
    paramMultipartFile = new SynthesizingMethodParameter(method, 3);
    paramMultipartFileList = new SynthesizingMethodParameter(method, 4);
    paramMultipartFileArray = new SynthesizingMethodParameter(method, 5);
    paramInt = new SynthesizingMethodParameter(method, 6);
    paramMultipartFileNotAnnot = new SynthesizingMethodParameter(method, 7);
    paramMultipartFileNotAnnot.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
    paramPart = new SynthesizingMethodParameter(method, 8);
    paramPart.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
    paramPartList = new SynthesizingMethodParameter(method, 9);
    paramPartArray = new SynthesizingMethodParameter(method, 10);
    paramRequestParamAnnot = new SynthesizingMethodParameter(method, 11);
    optionalMultipartFile = new SynthesizingMethodParameter(method, 12);
    optionalMultipartFile.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
    optionalMultipartFileList = new SynthesizingMethodParameter(method, 13);
    optionalMultipartFileList.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
    optionalPart = new SynthesizingMethodParameter(method, 14);
    optionalPart.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
    optionalPartList = new SynthesizingMethodParameter(method, 15);
    optionalPartList.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
    optionalRequestPart = new SynthesizingMethodParameter(method, 16);
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) LocalVariableTableParameterNameDiscoverer(org.springframework.core.LocalVariableTableParameterNameDiscoverer) SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) Method(java.lang.reflect.Method) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

Example 70 with HttpMessageConverter

use of org.springframework.http.converter.HttpMessageConverter in project spring-framework by spring-projects.

the class RequestResponseBodyMethodProcessorTests method resolveArgumentRawTypeFromParameterizedType.

@Test
public void resolveArgumentRawTypeFromParameterizedType() throws Exception {
    String content = "fruit=apple&vegetable=kale";
    this.servletRequest.setMethod("GET");
    this.servletRequest.setContent(content.getBytes("UTF-8"));
    this.servletRequest.setContentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE);
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new AllEncompassingFormHttpMessageConverter());
    RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
    @SuppressWarnings("unchecked") MultiValueMap<String, String> result = (MultiValueMap<String, String>) processor.resolveArgument(paramMultiValueMap, container, request, factory);
    assertNotNull(result);
    assertEquals("apple", result.getFirst("fruit"));
    assertEquals("kale", result.getFirst("vegetable"));
}
Also used : ArrayList(java.util.ArrayList) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) MultiValueMap(org.springframework.util.MultiValueMap) Test(org.junit.Test)

Aggregations

HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)95 ArrayList (java.util.ArrayList)66 Test (org.junit.Test)62 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)59 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)51 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)37 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)33 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)32 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)30 RestTemplate (org.springframework.web.client.RestTemplate)29 Method (java.lang.reflect.Method)25 MethodParameter (org.springframework.core.MethodParameter)25 HandlerMethod (org.springframework.web.method.HandlerMethod)24 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)14 SerDeHttpMessageConverter (com.kixeye.chassis.transport.http.SerDeHttpMessageConverter)13 MessageSerDe (com.kixeye.chassis.transport.serde.MessageSerDe)13 URI (java.net.URI)13 HashMap (java.util.HashMap)13 MapPropertySource (org.springframework.core.env.MapPropertySource)13 StandardEnvironment (org.springframework.core.env.StandardEnvironment)13