use of org.springframework.http.converter.StringHttpMessageConverter in project codekvast by crispab.
the class DashboardApiControllerTest method setup.
@BeforeEach
public void setup() {
MockitoAnnotations.openMocks(this);
DashboardApiController dashboardApiController = new DashboardApiController(dashboardService, settings);
this.mockMvc = MockMvcBuilders.standaloneSetup(dashboardApiController).setMessageConverters(new GsonHttpMessageConverter(), new StringHttpMessageConverter()).build();
}
use of org.springframework.http.converter.StringHttpMessageConverter in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method responseBodyNoAcceptableMediaType.
@Test
public void responseBodyNoAcceptableMediaType() throws ServletException, IOException {
initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
@Override
public void initialize(GenericWebApplicationContext wac) {
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
StringHttpMessageConverter converter = new StringHttpMessageConverter();
adapterDef.getPropertyValues().add("messageConverters", converter);
wac.registerBeanDefinition("handlerAdapter", adapterDef);
}
}, RequestResponseBodyProducesController.class);
MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
String requestBody = "Hello World";
request.setContent(requestBody.getBytes("UTF-8"));
request.addHeader("Content-Type", "text/plain; charset=utf-8");
request.addHeader("Accept", "application/pdf, application/msword");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals(406, response.getStatus());
}
use of org.springframework.http.converter.StringHttpMessageConverter in project spring-framework by spring-projects.
the class ServletInvocableHandlerMethodTests method responseEntityRawTypeWithNullBody.
// SPR-12287 (16/Oct/14 comments)
@Test
public void responseEntityRawTypeWithNullBody() throws Exception {
List<HttpMessageConverter<?>> converters = Collections.singletonList(new StringHttpMessageConverter());
List<Object> advice = Collections.singletonList(mock(ResponseBodyAdvice.class));
HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters, null, advice);
this.returnValueHandlers.addHandler(processor);
ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new ResponseEntityHandler(), "handleRawType");
handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer);
assertEquals(200, this.response.getStatus());
assertEquals("", this.response.getContentAsString());
}
use of org.springframework.http.converter.StringHttpMessageConverter in project spring-framework by spring-projects.
the class ServletInvocableHandlerMethodTests method wrapConcurrentResult_ResponseBody.
private void wrapConcurrentResult_ResponseBody(Object handler, Object result, Class<?> expectedReturnType) throws Exception {
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new StringHttpMessageConverter());
this.returnValueHandlers.addHandler(new ModelAndViewMethodReturnValueHandler());
this.returnValueHandlers.addHandler(new RequestResponseBodyMethodProcessor(converters));
ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(handler, "handle");
handlerMethod = handlerMethod.wrapConcurrentResult(result);
handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer);
assertEquals((result != null ? result.toString() : ""), this.response.getContentAsString());
assertEquals(expectedReturnType, handlerMethod.getReturnValueType(result).getParameterType());
}
use of org.springframework.http.converter.StringHttpMessageConverter in project littlefisher-system by littlefishercoder.
the class WebSpringMvcConfig method configureMessageConverters.
/**
* Description: 消息转换器配置
*
* @param converters converters
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper om = jacksonConverter.getObjectMapper();
om.setDateFormat(new SimpleDateFormat(DateUtil.DATETIME_FORMAT_1));
om.setLocale(Locale.CHINA);
om.setTimeZone(TimeZone.getTimeZone("GMT+8"));
List<MediaType> mediaTypeList = Lists.newArrayList();
mediaTypeList.add(new MediaType(MediaType.APPLICATION_JSON, Charsets.UTF_8));
mediaTypeList.add(new MediaType(MediaType.TEXT_HTML, Charsets.UTF_8));
jacksonConverter.setSupportedMediaTypes(mediaTypeList);
StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(Charsets.UTF_8);
stringConverter.setWriteAcceptCharset(false);
// 保持以下顺序
// "application/json" "application/*+json"
converters.add(jacksonConverter);
// "application/xml" "text/xml" "application/*+xml"
converters.add(new Jaxb2RootElementHttpMessageConverter());
// "application/xml" "text/xml" "application/*+xml"
converters.add(new SourceHttpMessageConverter<>());
// "application/x-www-form-urlencoded" "multipart/form-data"
converters.add(new AllEncompassingFormHttpMessageConverter());
// "application/octet-stream" "*/*"
converters.add(new ByteArrayHttpMessageConverter());
// "text/plain" "*/*"
converters.add(stringConverter);
// "*/*"
converters.add(new ResourceHttpMessageConverter());
}
Aggregations