Search in sources :

Example 16 with MethodParameter

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

the class RequestResponseBodyMethodProcessorTests method jacksonJsonViewWithResponseBodyAndXmlMessageConverter.

// SPR-12149
@Test
public void jacksonJsonViewWithResponseBodyAndXmlMessageConverter() throws Throwable {
    Method method = JacksonController.class.getMethod("handleResponseBody");
    HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
    MethodParameter methodReturnType = handlerMethod.getReturnType();
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new MappingJackson2XmlHttpMessageConverter());
    RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters, null, Collections.singletonList(new JsonViewResponseBodyAdvice()));
    Object returnValue = new JacksonController().handleResponseBody();
    processor.handleReturnValue(request, handlerMethod, returnValue);
    String content = this.servletResponse.getContentAsString();
    assertThat(content.contains("<withView1>with</withView1>")).isFalse();
    assertThat(content.contains("<withView2>with</withView2>")).isTrue();
    assertThat(content.contains("<withoutView>without</withoutView>")).isFalse();
}
Also used : 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) ArrayList(java.util.ArrayList) JsonViewResponseBodyAdvice(cn.taketoday.web.handler.method.JsonViewResponseBodyAdvice) 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 17 with MethodParameter

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

the class RequestResponseBodyMethodProcessorTests method supportsReturnTypeResponseBodyOnType.

@Test
public void supportsReturnTypeResponseBodyOnType() throws Throwable {
    Method method = ResponseBodyController.class.getMethod("handle");
    MethodParameter returnType = new MethodParameter(method, -1);
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new StringHttpMessageConverter());
    RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
    assertThat(processor.supportsHandlerMethod(new HandlerMethod(this, method))).as("Failed to recognize type-level @ResponseBody").isTrue();
}
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) Method(java.lang.reflect.Method) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) MethodParameter(cn.taketoday.core.MethodParameter) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 18 with MethodParameter

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

the class RequestResponseBodyMethodProcessorTests method jacksonSubType.

// SPR-13318
@Test
public void jacksonSubType() throws Throwable {
    Method method = JacksonController.class.getMethod("handleSubType");
    HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
    MethodParameter methodReturnType = handlerMethod.getReturnType();
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new MappingJackson2HttpMessageConverter());
    RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
    Object returnValue = new JacksonController().handleSubType();
    processor.handleReturnValue(request, handlerMethod, returnValue);
    String content = this.servletResponse.getContentAsString();
    assertThat(content.contains("\"id\":123")).isTrue();
    assertThat(content.contains("\"name\":\"foo\"")).isTrue();
}
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) 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 19 with MethodParameter

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

the class RequestResponseBodyMethodProcessorTests method jacksonJsonViewWithResponseEntityAndXmlMessageConverter.

// SPR-12149
@Test
public void jacksonJsonViewWithResponseEntityAndXmlMessageConverter() throws Throwable {
    Method method = JacksonController.class.getMethod("handleResponseEntity");
    HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
    MethodParameter methodReturnType = handlerMethod.getReturnType();
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new MappingJackson2XmlHttpMessageConverter());
    HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters, null, Collections.singletonList(new JsonViewResponseBodyAdvice()), null);
    Object returnValue = new JacksonController().handleResponseEntity();
    processor.handleReturnValue(request, handlerMethod, returnValue);
    String content = this.servletResponse.getContentAsString();
    assertThat(content.contains("<withView1>with</withView1>")).isFalse();
    assertThat(content.contains("<withView2>with</withView2>")).isTrue();
    assertThat(content.contains("<withoutView>without</withoutView>")).isFalse();
}
Also used : 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) ArrayList(java.util.ArrayList) JsonViewResponseBodyAdvice(cn.taketoday.web.handler.method.JsonViewResponseBodyAdvice) 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 20 with MethodParameter

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

the class RequestResponseBodyMethodProcessorTests method resolveArgumentTypeVariableWithNonGenericConverter.

// SPR-11225
@Test
public void resolveArgumentTypeVariableWithNonGenericConverter() throws Throwable {
    Method method = MyParameterizedController.class.getMethod("handleDto", Identifiable.class);
    HandlerMethod handlerMethod = new HandlerMethod(new MySimpleParameterizedController(), method);
    MethodParameter methodParam = handlerMethod.getMethodParameters()[0];
    String content = "{\"name\" : \"Jad\"}";
    this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
    this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    HttpMessageConverter<Object> target = new MappingJackson2HttpMessageConverter();
    HttpMessageConverter<?> proxy = ProxyFactory.getProxy(HttpMessageConverter.class, new SingletonTargetSource(target));
    converters.add(proxy);
    RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
    SimpleBean result = (SimpleBean) processor.resolveArgument(request, new ResolvableMethodParameter(methodParam));
    assertThat(result).isNotNull();
    assertThat(result.getName()).isEqualTo("Jad");
}
Also used : MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) SingletonTargetSource(cn.taketoday.aop.target.SingletonTargetSource) 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)

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