Search in sources :

Example 21 with MethodParameter

use of cn.taketoday.core.MethodParameter in project today-framework by TAKETODAY.

the class RequestResponseBodyMethodProcessorTests method resolveHttpEntityArgumentWithJacksonJsonViewAndXmlMessageConverter.

// SPR-12501
@Test
public void resolveHttpEntityArgumentWithJacksonJsonViewAndXmlMessageConverter() throws Throwable {
    String content = "<root>" + "<withView1>with</withView1>" + "<withView2>with</withView2>" + "<withoutView>without</withoutView></root>";
    this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
    this.servletRequest.setContentType(MediaType.APPLICATION_XML_VALUE);
    Method method = JacksonController.class.getMethod("handleHttpEntity", HttpEntity.class);
    HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
    MethodParameter methodParameter = handlerMethod.getMethodParameters()[0];
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new MappingJackson2XmlHttpMessageConverter());
    HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters, null, Collections.singletonList(new JsonViewRequestBodyAdvice()), null);
    @SuppressWarnings("unchecked") HttpEntity<JacksonViewBean> result = (HttpEntity<JacksonViewBean>) processor.resolveArgument(request, new ResolvableMethodParameter(methodParameter));
    assertThat(result).isNotNull();
    assertThat(result.getBody()).isNotNull();
    assertThat(result.getBody().getWithView1()).isEqualTo("with");
    assertThat(result.getBody().getWithView2()).isNull();
    assertThat(result.getBody().getWithoutView()).isNull();
}
Also used : JsonViewRequestBodyAdvice(cn.taketoday.web.handler.method.JsonViewRequestBodyAdvice) HttpEntity(cn.taketoday.http.HttpEntity) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) MappingJackson2XmlHttpMessageConverter(cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter) HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(cn.taketoday.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) AllEncompassingFormHttpMessageConverter(cn.taketoday.http.converter.AllEncompassingFormHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter) MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) ResourceHttpMessageConverter(cn.taketoday.http.converter.ResourceHttpMessageConverter) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) Test(org.junit.jupiter.api.Test)

Example 22 with MethodParameter

use of cn.taketoday.core.MethodParameter in project today-framework by TAKETODAY.

the class RequestResponseBodyMethodProcessorTests method resolveArgumentTypeVariableWithGenericInterface.

// SPR-14520
@Test
public void resolveArgumentTypeVariableWithGenericInterface() throws Throwable {
    this.servletRequest.setContent("\"foo\"".getBytes(StandardCharsets.UTF_8));
    this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
    Method method = MyControllerImplementingInterface.class.getMethod("handle", Object.class);
    HandlerMethod handlerMethod = new HandlerMethod(new MyControllerImplementingInterface(), method);
    MethodParameter methodParameter = handlerMethod.getMethodParameters()[0];
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new MappingJackson2HttpMessageConverter());
    RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
    assertThat(processor.supportsParameter(new ResolvableMethodParameter(methodParameter))).isTrue();
    String value = (String) processor.readWithMessageConverters(this.request, methodParameter, methodParameter.getGenericParameterType());
    assertThat(value).isEqualTo("foo");
}
Also used : MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(cn.taketoday.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) AllEncompassingFormHttpMessageConverter(cn.taketoday.http.converter.AllEncompassingFormHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter) MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) ResourceHttpMessageConverter(cn.taketoday.http.converter.ResourceHttpMessageConverter) ArrayList(java.util.ArrayList) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) Method(java.lang.reflect.Method) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) MethodParameter(cn.taketoday.core.MethodParameter) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 23 with MethodParameter

use of cn.taketoday.core.MethodParameter in project today-framework by TAKETODAY.

the class RequestResponseBodyMethodProcessorTests method resolveArgumentWithJacksonJsonView.

// SPR-12501
@Test
public void resolveArgumentWithJacksonJsonView() throws Throwable {
    String content = "{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}";
    this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
    this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
    Method method = JacksonController.class.getMethod("handleRequestBody", JacksonViewBean.class);
    HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
    MethodParameter methodParameter = handlerMethod.getMethodParameters()[0];
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new MappingJackson2HttpMessageConverter());
    RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters, null, Collections.singletonList(new JsonViewRequestBodyAdvice()));
    JacksonViewBean result = (JacksonViewBean) processor.resolveArgument(request, new ResolvableMethodParameter(methodParameter));
    assertThat(result).isNotNull();
    assertThat(result.getWithView1()).isEqualTo("with");
    assertThat(result.getWithView2()).isNull();
    assertThat(result.getWithoutView()).isNull();
}
Also used : MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) JsonViewRequestBodyAdvice(cn.taketoday.web.handler.method.JsonViewRequestBodyAdvice) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(cn.taketoday.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) AllEncompassingFormHttpMessageConverter(cn.taketoday.http.converter.AllEncompassingFormHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter) MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) ResourceHttpMessageConverter(cn.taketoday.http.converter.ResourceHttpMessageConverter) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) Test(org.junit.jupiter.api.Test)

Example 24 with MethodParameter

use of cn.taketoday.core.MethodParameter in project today-framework by TAKETODAY.

the class RequestResponseBodyMethodProcessorTests method handleReturnValueImage.

// SPR-12894
@Test
public void handleReturnValueImage() throws Throwable {
    this.servletRequest.addHeader("Accept", "*/*");
    Method method = getClass().getDeclaredMethod("getImage");
    MethodParameter returnType = new MethodParameter(method, -1);
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new ResourceHttpMessageConverter());
    RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
    ClassPathResource resource = new ClassPathResource("logo.jpg", getClass());
    processor.writeWithMessageConverters(resource, returnType, this.request);
    assertThat(this.servletResponse.getHeader("Content-Type")).isEqualTo("image/jpeg");
}
Also used : HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(cn.taketoday.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) AllEncompassingFormHttpMessageConverter(cn.taketoday.http.converter.AllEncompassingFormHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter) MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) ResourceHttpMessageConverter(cn.taketoday.http.converter.ResourceHttpMessageConverter) ArrayList(java.util.ArrayList) ResourceHttpMessageConverter(cn.taketoday.http.converter.ResourceHttpMessageConverter) Method(java.lang.reflect.Method) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) MethodParameter(cn.taketoday.core.MethodParameter) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) ClassPathResource(cn.taketoday.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 25 with MethodParameter

use of cn.taketoday.core.MethodParameter in project today-framework by TAKETODAY.

the class ReactiveTypeHandlerTests method handleValue.

private ResponseBodyEmitter handleValue(Object returnValue, Class<?> asyncType, ResolvableType genericType) throws Exception {
    BindingContext mavContainer = new BindingContext();
    MethodParameter returnType = on(TestController.class).resolveReturnType(asyncType, genericType);
    webRequest.setBindingContext(mavContainer);
    return this.handler.handleValue(returnValue, returnType, this.webRequest);
}
Also used : BindingContext(cn.taketoday.web.BindingContext) MethodParameter(cn.taketoday.core.MethodParameter)

Aggregations

MethodParameter (cn.taketoday.core.MethodParameter)97 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)43 Method (java.lang.reflect.Method)43 Test (org.junit.jupiter.api.Test)42 ArrayList (java.util.ArrayList)30 HandlerMethod (cn.taketoday.web.handler.method.HandlerMethod)28 MappingJackson2HttpMessageConverter (cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter)24 HttpMessageConverter (cn.taketoday.http.converter.HttpMessageConverter)23 ByteArrayHttpMessageConverter (cn.taketoday.http.converter.ByteArrayHttpMessageConverter)22 StringHttpMessageConverter (cn.taketoday.http.converter.StringHttpMessageConverter)22 Nullable (cn.taketoday.lang.Nullable)21 TypeDescriptor (cn.taketoday.core.TypeDescriptor)18 AllEncompassingFormHttpMessageConverter (cn.taketoday.http.converter.AllEncompassingFormHttpMessageConverter)17 ResourceHttpMessageConverter (cn.taketoday.http.converter.ResourceHttpMessageConverter)17 MappingJackson2XmlHttpMessageConverter (cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter)17 TypeMismatchException (cn.taketoday.beans.TypeMismatchException)9 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)9 BeforeEach (org.junit.jupiter.api.BeforeEach)9 DependencyDescriptor (cn.taketoday.beans.factory.config.DependencyDescriptor)8 SynthesizingMethodParameter (cn.taketoday.core.annotation.SynthesizingMethodParameter)8