Search in sources :

Example 16 with MethodParameter

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

the class ModelAndViewResolverMethodReturnValueHandlerTests method handleNull.

@Test
public void handleNull() throws Exception {
    MethodParameter returnType = new MethodParameter(getClass().getDeclaredMethod("testBeanReturnValue"), -1);
    handler.handleReturnValue(null, returnType, mavContainer, request);
    assertNull(mavContainer.getView());
    assertNull(mavContainer.getViewName());
    assertTrue(mavContainer.getModel().isEmpty());
}
Also used : MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 17 with MethodParameter

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

the class RequestResponseBodyMethodProcessorTests method resolveParameterizedWithTypeVariableArgument.

// SPR-14470
@Test
public void resolveParameterizedWithTypeVariableArgument() throws Exception {
    Method method = MyParameterizedControllerWithList.class.getMethod("handleDto", List.class);
    HandlerMethod handlerMethod = new HandlerMethod(new MySimpleParameterizedControllerWithList(), method);
    MethodParameter methodParam = handlerMethod.getMethodParameters()[0];
    String content = "[{\"name\" : \"Jad\"}, {\"name\" : \"Robert\"}]";
    this.servletRequest.setContent(content.getBytes("UTF-8"));
    this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new MappingJackson2HttpMessageConverter());
    RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
    @SuppressWarnings("unchecked") List<SimpleBean> result = (List<SimpleBean>) processor.resolveArgument(methodParam, container, request, factory);
    assertNotNull(result);
    assertEquals("Jad", result.get(0).getName());
    assertEquals("Robert", result.get(1).getName());
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) HandlerMethod(org.springframework.web.method.HandlerMethod) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) List(java.util.List) ArrayList(java.util.ArrayList) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 18 with MethodParameter

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

the class RequestResponseBodyMethodProcessorTests method jacksonSubType.

// SPR-13318
@Test
public void jacksonSubType() throws Exception {
    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(returnValue, methodReturnType, this.container, this.request);
    String content = this.servletResponse.getContentAsString();
    assertTrue(content.contains("\"id\":123"));
    assertTrue(content.contains("\"name\":\"foo\""));
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 19 with MethodParameter

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

the class ResponseBodyEmitterReturnValueHandlerTests method sseEmitter.

@Test
public void sseEmitter() throws Exception {
    MethodParameter returnType = returnType("handleSse");
    SseEmitter emitter = new SseEmitter();
    handleReturnValue(emitter, returnType);
    assertTrue(this.request.isAsyncStarted());
    assertEquals(200, this.response.getStatus());
    assertEquals("text/event-stream;charset=UTF-8", this.response.getContentType());
    SimpleBean bean1 = new SimpleBean();
    bean1.setId(1L);
    bean1.setName("Joe");
    SimpleBean bean2 = new SimpleBean();
    bean2.setId(2L);
    bean2.setName("John");
    emitter.send(SseEmitter.event().comment("a test").name("update").id("1").reconnectTime(5000L).data(bean1).data(bean2));
    assertEquals(":a test\n" + "event:update\n" + "id:1\n" + "retry:5000\n" + "data:{\"id\":1,\"name\":\"Joe\"}\n" + "data:{\"id\":2,\"name\":\"John\"}\n" + "\n", this.response.getContentAsString());
}
Also used : MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 20 with MethodParameter

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

the class ResponseBodyEmitterReturnValueHandlerTests method responseEntitySse.

@Test
public void responseEntitySse() throws Exception {
    MethodParameter returnType = returnType("handleResponseEntitySse");
    ResponseEntity<SseEmitter> entity = ResponseEntity.ok().header("foo", "bar").body(new SseEmitter());
    handleReturnValue(entity, returnType);
    assertTrue(this.request.isAsyncStarted());
    assertEquals(200, this.response.getStatus());
    assertEquals("text/event-stream;charset=UTF-8", this.response.getContentType());
    assertEquals("bar", this.response.getHeader("foo"));
}
Also used : MethodParameter(org.springframework.core.MethodParameter) 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