Search in sources :

Example 46 with StringHttpMessageConverter

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();
}
Also used : GsonHttpMessageConverter(org.springframework.http.converter.json.GsonHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 47 with StringHttpMessageConverter

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());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 48 with StringHttpMessageConverter

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());
}
Also used : StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) Test(org.junit.Test)

Example 49 with StringHttpMessageConverter

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());
}
Also used : ArrayList(java.util.ArrayList) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter)

Example 50 with StringHttpMessageConverter

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());
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) Jaxb2RootElementHttpMessageConverter(org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) MediaType(org.springframework.http.MediaType) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) SimpleDateFormat(java.text.SimpleDateFormat) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter)

Aggregations

StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)93 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)48 ArrayList (java.util.ArrayList)38 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)27 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)24 RestTemplate (org.springframework.web.client.RestTemplate)23 Test (org.junit.jupiter.api.Test)21 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)18 Test (org.junit.Test)16 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)16 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)11 MediaType (org.springframework.http.MediaType)9 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)9 SerDeHttpMessageConverter (com.kixeye.chassis.transport.http.SerDeHttpMessageConverter)7 MessageSerDe (com.kixeye.chassis.transport.serde.MessageSerDe)7 URI (java.net.URI)7 HashMap (java.util.HashMap)7 MethodParameter (org.springframework.core.MethodParameter)7 MapPropertySource (org.springframework.core.env.MapPropertySource)7 StandardEnvironment (org.springframework.core.env.StandardEnvironment)7