Search in sources :

Example 76 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class MessageReaderArgumentResolverTests method missingContentType.

@Test
public void missingContentType() throws Exception {
    ServerWebExchange exchange = post("/path").body("{\"bar\":\"BARBAR\",\"foo\":\"FOOFOO\"}").toExchange();
    ResolvableType type = forClassWithGenerics(Mono.class, TestBean.class);
    MethodParameter param = this.testMethod.arg(type);
    Mono<Object> result = this.resolver.readBody(param, true, this.bindingContext, exchange);
    StepVerifier.create(result).expectError(UnsupportedMediaTypeStatusException.class).verify();
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) UnsupportedMediaTypeStatusException(org.springframework.web.server.UnsupportedMediaTypeStatusException) ResolvableType(org.springframework.core.ResolvableType) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 77 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class MessageWriterResultHandlerTests method jacksonTypeOfListElement.

// SPR-12811
@Test
public void jacksonTypeOfListElement() throws Exception {
    MethodParameter returnType = on(TestController.class).resolveReturnType(List.class, ParentClass.class);
    List<ParentClass> body = Arrays.asList(new Foo("foo"), new Bar("bar"));
    this.resultHandler.writeBody(body, returnType, this.exchange).block(Duration.ofSeconds(5));
    assertEquals(APPLICATION_JSON_UTF8, this.exchange.getResponse().getHeaders().getContentType());
    assertResponseBody("[{\"type\":\"foo\",\"parentProperty\":\"foo\"}," + "{\"type\":\"bar\",\"parentProperty\":\"bar\"}]");
}
Also used : MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 78 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class MessageWriterResultHandlerTests method jacksonTypeWithSubTypeOfListElement.

// SPR-13318
@Test
public void jacksonTypeWithSubTypeOfListElement() throws Exception {
    MethodParameter returnType = on(TestController.class).resolveReturnType(List.class, Identifiable.class);
    List<SimpleBean> body = Arrays.asList(new SimpleBean(123L, "foo"), new SimpleBean(456L, "bar"));
    this.resultHandler.writeBody(body, returnType, this.exchange).block(Duration.ofSeconds(5));
    assertEquals(APPLICATION_JSON_UTF8, this.exchange.getResponse().getHeaders().getContentType());
    assertResponseBody("[{\"id\":123,\"name\":\"foo\"},{\"id\":456,\"name\":\"bar\"}]");
}
Also used : MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 79 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class MessageWriterResultHandlerTests method jacksonTypeWithSubType.

// SPR-13318
@Test
public void jacksonTypeWithSubType() throws Exception {
    SimpleBean body = new SimpleBean(123L, "foo");
    MethodParameter type = on(TestController.class).resolveReturnType(Identifiable.class);
    this.resultHandler.writeBody(body, type, this.exchange).block(Duration.ofSeconds(5));
    assertEquals(APPLICATION_JSON_UTF8, this.exchange.getResponse().getHeaders().getContentType());
    assertResponseBody("{\"id\":123,\"name\":\"foo\"}");
}
Also used : MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 80 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class MessageWriterResultHandlerTests method useDefaultContentType.

// SPR-12894
@Test
public void useDefaultContentType() throws Exception {
    Resource body = new ClassPathResource("logo.png", getClass());
    MethodParameter type = on(TestController.class).resolveReturnType(Resource.class);
    this.resultHandler.writeBody(body, type, this.exchange).block(Duration.ofSeconds(5));
    assertEquals("image/x-png", this.exchange.getResponse().getHeaders().getFirst("Content-Type"));
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MethodParameter(org.springframework.core.MethodParameter) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Aggregations

MethodParameter (org.springframework.core.MethodParameter)322 Test (org.junit.Test)251 Method (java.lang.reflect.Method)65 ArrayList (java.util.ArrayList)35 RequestParam (org.springframework.web.bind.annotation.RequestParam)30 HandlerMethod (org.springframework.web.method.HandlerMethod)28 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)27 Before (org.junit.Before)25 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)25 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)23 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)23 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)23 ResolvableType (org.springframework.core.ResolvableType)21 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)21 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)21 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)20 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)20 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)20 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)20 Mono (reactor.core.publisher.Mono)19