Search in sources :

Example 86 with StringHttpMessageConverter

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

the class RouterFunctionMapping method initMessageConverters.

/**
 * Initializes a default set of {@linkplain HttpMessageConverter message converters}.
 */
private void initMessageConverters() {
    List<HttpMessageConverter<?>> messageConverters = new ArrayList<>(4);
    messageConverters.add(new ByteArrayHttpMessageConverter());
    messageConverters.add(new StringHttpMessageConverter());
    if (!shouldIgnoreXml) {
        try {
            messageConverters.add(new SourceHttpMessageConverter<>());
        } catch (Error err) {
        // Ignore when no TransformerFactory implementation is available
        }
    }
    messageConverters.add(new AllEncompassingFormHttpMessageConverter());
    this.messageConverters = messageConverters;
}
Also used : ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) SourceHttpMessageConverter(org.springframework.http.converter.xml.SourceHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ArrayList(java.util.ArrayList) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter)

Example 87 with StringHttpMessageConverter

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

the class HttpEntityMethodProcessorTests method handleReturnValueCharSequence.

// SPR-13423
@Test
public void handleReturnValueCharSequence() throws Exception {
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new ByteArrayHttpMessageConverter());
    converters.add(new StringHttpMessageConverter());
    Method method = getClass().getDeclaredMethod("handle");
    MethodParameter returnType = new MethodParameter(method, -1);
    ResponseEntity<StringBuilder> returnValue = ResponseEntity.ok(new StringBuilder("Foo"));
    HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters);
    processor.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
    assertThat(servletResponse.getHeader("Content-Type")).isEqualTo("text/plain;charset=ISO-8859-1");
    assertThat(servletResponse.getContentAsString()).isEqualTo("Foo");
}
Also used : ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ArrayList(java.util.ArrayList) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) Test(org.junit.jupiter.api.Test)

Example 88 with StringHttpMessageConverter

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

the class DefaultServerResponseBuilderTests method bodyPublisher.

@Test
public void bodyPublisher() throws Exception {
    String body = "foo";
    Publisher<String> publisher = Mono.just(body);
    ServerResponse response = ServerResponse.ok().body(publisher);
    MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", "https://example.com");
    MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    mockRequest.setAsyncSupported(true);
    ServerResponse.Context context = () -> Collections.singletonList(new StringHttpMessageConverter());
    ModelAndView mav = response.writeTo(mockRequest, mockResponse, context);
    assertThat(mav).isNull();
    assertThat(mockResponse.getContentAsString()).isEqualTo(body);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) Test(org.junit.jupiter.api.Test)

Example 89 with StringHttpMessageConverter

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

the class DefaultServerResponseBuilderTests method bodyCompletionStage.

@Test
public void bodyCompletionStage() throws Exception {
    String body = "foo";
    CompletionStage<String> completionStage = CompletableFuture.completedFuture(body);
    ServerResponse response = ServerResponse.ok().body(completionStage);
    MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", "https://example.com");
    MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    mockRequest.setAsyncSupported(true);
    ServerResponse.Context context = () -> Collections.singletonList(new StringHttpMessageConverter());
    ModelAndView mav = response.writeTo(mockRequest, mockResponse, context);
    assertThat(mav).isNull();
    assertThat(mockResponse.getContentAsString()).isEqualTo(body);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) Test(org.junit.jupiter.api.Test)

Example 90 with StringHttpMessageConverter

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

the class ContentRequestMatchersIntegrationTests method setup.

@BeforeEach
public void setup() {
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new StringHttpMessageConverter());
    converters.add(new MappingJackson2HttpMessageConverter());
    this.restTemplate = new RestTemplate();
    this.restTemplate.setMessageConverters(converters);
    this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ArrayList(java.util.ArrayList) RestTemplate(org.springframework.web.client.RestTemplate) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) BeforeEach(org.junit.jupiter.api.BeforeEach)

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