use of org.springframework.http.converter.HttpMessageConverter in project spring-framework by spring-projects.
the class DelegatingWebMvcConfigurationTests method configureMessageConverters.
@Test
public void configureMessageConverters() {
final HttpMessageConverter customConverter = mock(HttpMessageConverter.class);
final StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
List<WebMvcConfigurer> configurers = new ArrayList<>();
configurers.add(new WebMvcConfigurerAdapter() {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(stringConverter);
}
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(0, customConverter);
}
});
delegatingConfig = new DelegatingWebMvcConfiguration();
delegatingConfig.setConfigurers(configurers);
RequestMappingHandlerAdapter adapter = delegatingConfig.requestMappingHandlerAdapter();
assertEquals("Only one custom converter should be registered", 2, adapter.getMessageConverters().size());
assertSame(customConverter, adapter.getMessageConverters().get(0));
assertSame(stringConverter, adapter.getMessageConverters().get(1));
}
use of org.springframework.http.converter.HttpMessageConverter in project spring-framework by spring-projects.
the class TestPathHelper method verifyMessageConverters.
@SuppressWarnings("unchecked")
private void verifyMessageConverters(Object bean, boolean hasDefaultRegistrations) {
assertNotNull(bean);
Object value = new DirectFieldAccessor(bean).getPropertyValue("messageConverters");
assertNotNull(value);
assertTrue(value instanceof List);
List<HttpMessageConverter<?>> converters = (List<HttpMessageConverter<?>>) value;
if (hasDefaultRegistrations) {
assertTrue("Default and custom converter expected", converters.size() > 2);
} else {
assertTrue("Only custom converters expected", converters.size() == 2);
}
assertTrue(converters.get(0) instanceof StringHttpMessageConverter);
assertTrue(converters.get(1) instanceof ResourceHttpMessageConverter);
}
use of org.springframework.http.converter.HttpMessageConverter in project spring-framework by spring-projects.
the class HttpEntityMethodProcessorMockTests method setup.
@Before
@SuppressWarnings("unchecked")
public void setup() throws Exception {
dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
stringHttpMessageConverter = mock(HttpMessageConverter.class);
given(stringHttpMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
resourceMessageConverter = mock(HttpMessageConverter.class);
given(resourceMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL));
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(stringHttpMessageConverter);
converters.add(resourceMessageConverter);
processor = new HttpEntityMethodProcessor(converters);
reset(stringHttpMessageConverter);
reset(resourceMessageConverter);
Method handle1 = getClass().getMethod("handle1", HttpEntity.class, ResponseEntity.class, Integer.TYPE, RequestEntity.class);
paramHttpEntity = new MethodParameter(handle1, 0);
paramRequestEntity = new MethodParameter(handle1, 3);
paramResponseEntity = new MethodParameter(handle1, 1);
paramInt = new MethodParameter(handle1, 2);
returnTypeResponseEntity = new MethodParameter(handle1, -1);
returnTypeResponseEntityProduces = new MethodParameter(getClass().getMethod("handle4"), -1);
returnTypeHttpEntity = new MethodParameter(getClass().getMethod("handle2", HttpEntity.class), -1);
returnTypeHttpEntitySubclass = new MethodParameter(getClass().getMethod("handle2x", HttpEntity.class), -1);
returnTypeInt = new MethodParameter(getClass().getMethod("handle3"), -1);
returnTypeResponseEntityResource = new MethodParameter(getClass().getMethod("handle5"), -1);
mavContainer = new ModelAndViewContainer();
servletRequest = new MockHttpServletRequest("GET", "/foo");
servletResponse = new MockHttpServletResponse();
webRequest = new ServletWebRequest(servletRequest, servletResponse);
}
use of org.springframework.http.converter.HttpMessageConverter in project spring-framework by spring-projects.
the class HttpEntityMethodProcessorTests method resolveGenericArgument.
@Test
public void resolveGenericArgument() throws Exception {
String content = "[{\"name\" : \"Jad\"}, {\"name\" : \"Robert\"}]";
this.servletRequest.setContent(content.getBytes("UTF-8"));
this.servletRequest.setContentType("application/json");
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new MappingJackson2HttpMessageConverter());
HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters);
@SuppressWarnings("unchecked") HttpEntity<List<SimpleBean>> result = (HttpEntity<List<SimpleBean>>) processor.resolveArgument(paramList, mavContainer, webRequest, binderFactory);
assertNotNull(result);
assertEquals("Jad", result.getBody().get(0).getName());
assertEquals("Robert", result.getBody().get(1).getName());
}
use of org.springframework.http.converter.HttpMessageConverter in project spring-framework by spring-projects.
the class HttpEntityMethodProcessorTests method jacksonTypeInfoList.
// SPR-12811
@Test
public void jacksonTypeInfoList() throws Exception {
Method method = JacksonController.class.getMethod("handleList");
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);
Object returnValue = new JacksonController().handleList();
processor.handleReturnValue(returnValue, methodReturnType, this.mavContainer, this.webRequest);
String content = this.servletResponse.getContentAsString();
assertTrue(content.contains("\"type\":\"foo\""));
assertTrue(content.contains("\"type\":\"bar\""));
}
Aggregations