Java Code Examples for org.springframework.web.method.HandlerMethod#getReturnType()

The following examples show how to use org.springframework.web.method.HandlerMethod#getReturnType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpEntityMethodProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test  // SPR-12811
public void jacksonTypeInfoList() throws Exception {
	Method method = JacksonController.class.getMethod("handleList");
	HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
	MethodParameter methodReturnType = handlerMethod.getReturnType();

	List<HttpMessageConverter<?>> converters = new ArrayList<>();
	converters.add(new MappingJackson2HttpMessageConverter());
	HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters);

	Object returnValue = new JacksonController().handleList();
	processor.handleReturnValue(returnValue, methodReturnType, this.mavContainer, this.webRequest);

	String content = this.servletResponse.getContentAsString();
	assertTrue(content.contains("\"type\":\"foo\""));
	assertTrue(content.contains("\"type\":\"bar\""));
}
 
Example 2
Source File: RequestResponseBodyMethodProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test  // SPR-13318
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\""));
}
 
Example 3
Source File: RequestResponseBodyMethodProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test  // SPR-13318
public void jacksonSubTypeList() throws Exception {
	Method method = JacksonController.class.getMethod("handleSubTypeList");
	HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
	MethodParameter methodReturnType = handlerMethod.getReturnType();

	List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
	converters.add(new MappingJackson2HttpMessageConverter());
	RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);

	Object returnValue = new JacksonController().handleSubTypeList();
	processor.handleReturnValue(returnValue, methodReturnType, this.mavContainer, this.webRequest);

	String content = this.servletResponse.getContentAsString();
	assertTrue(content.contains("\"id\":123"));
	assertTrue(content.contains("\"name\":\"foo\""));
	assertTrue(content.contains("\"id\":456"));
	assertTrue(content.contains("\"name\":\"bar\""));
}
 
Example 4
Source File: RequestResponseBodyMethodProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test  // SPR-12149
public void jacksonJsonViewWithResponseEntityAndXmlMessageConverter() throws Exception {
	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()));

	Object returnValue = new JacksonController().handleResponseEntity();
	processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request);

	String content = this.servletResponse.getContentAsString();
	assertFalse(content.contains("<withView1>with</withView1>"));
	assertTrue(content.contains("<withView2>with</withView2>"));
	assertFalse(content.contains("<withoutView>without</withoutView>"));
}
 
Example 5
Source File: RequestResponseBodyMethodProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test  // SPR-12149
public void jacksonJsonViewWithResponseBodyAndXmlMessageConverter() throws Exception {
	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(returnValue, methodReturnType, this.container, this.request);

	String content = this.servletResponse.getContentAsString();
	assertFalse(content.contains("<withView1>with</withView1>"));
	assertTrue(content.contains("<withView2>with</withView2>"));
	assertFalse(content.contains("<withoutView>without</withoutView>"));
}
 
Example 6
Source File: RequestResponseBodyMethodProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void jacksonJsonViewWithResponseBodyAndJsonMessageConverter() throws Exception {
	Method method = JacksonController.class.getMethod("handleResponseBody");
	HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
	MethodParameter methodReturnType = handlerMethod.getReturnType();

	List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
	converters.add(new MappingJackson2HttpMessageConverter());

	RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(
			converters, null, Collections.singletonList(new JsonViewResponseBodyAdvice()));

	Object returnValue = new JacksonController().handleResponseBody();
	processor.handleReturnValue(returnValue, methodReturnType, this.mavContainer, this.webRequest);

	String content = this.servletResponse.getContentAsString();
	assertFalse(content.contains("\"withView1\":\"with\""));
	assertTrue(content.contains("\"withView2\":\"with\""));
	assertFalse(content.contains("\"withoutView\":\"without\""));
}
 
Example 7
Source File: RequestResponseBodyMethodProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void jacksonJsonViewWithResponseBodyAndJsonMessageConverter() throws Exception {
	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 MappingJackson2HttpMessageConverter());

	RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(
			converters, null, Collections.singletonList(new JsonViewResponseBodyAdvice()));

	Object returnValue = new JacksonController().handleResponseBody();
	processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request);

	String content = this.servletResponse.getContentAsString();
	assertFalse(content.contains("\"withView1\":\"with\""));
	assertTrue(content.contains("\"withView2\":\"with\""));
	assertFalse(content.contains("\"withoutView\":\"without\""));
}
 
Example 8
Source File: HttpEntityMethodProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test  // SPR-12811
public void jacksonTypeInfoList() throws Exception {
	Method method = JacksonController.class.getMethod("handleList");
	HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
	MethodParameter methodReturnType = handlerMethod.getReturnType();

	List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
	converters.add(new MappingJackson2HttpMessageConverter());
	HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters);

	Object returnValue = new JacksonController().handleList();
	processor.handleReturnValue(returnValue, methodReturnType, this.mavContainer, this.webRequest);

	String content = this.servletResponse.getContentAsString();
	assertTrue(content.contains("\"type\":\"foo\""));
	assertTrue(content.contains("\"type\":\"bar\""));
}
 
Example 9
Source File: RequestResponseBodyMethodProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void jacksonJsonViewWithResponseEntityAndJsonMessageConverter() throws Exception {
	Method method = JacksonController.class.getMethod("handleResponseEntity");
	HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
	MethodParameter methodReturnType = handlerMethod.getReturnType();

	List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
	converters.add(new MappingJackson2HttpMessageConverter());

	HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(
			converters, null, Collections.singletonList(new JsonViewResponseBodyAdvice()));

	Object returnValue = new JacksonController().handleResponseEntity();
	processor.handleReturnValue(returnValue, methodReturnType, this.mavContainer, this.webRequest);

	String content = this.servletResponse.getContentAsString();
	assertFalse(content.contains("\"withView1\":\"with\""));
	assertTrue(content.contains("\"withView2\":\"with\""));
	assertFalse(content.contains("\"withoutView\":\"without\""));
}
 
Example 10
Source File: RequestResponseBodyMethodProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test  // SPR-13318
public void jacksonSubTypeList() throws Exception {
	Method method = JacksonController.class.getMethod("handleSubTypeList");
	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().handleSubTypeList();
	processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request);

	String content = this.servletResponse.getContentAsString();
	assertTrue(content.contains("\"id\":123"));
	assertTrue(content.contains("\"name\":\"foo\""));
	assertTrue(content.contains("\"id\":456"));
	assertTrue(content.contains("\"name\":\"bar\""));
}
 
Example 11
Source File: RequestResponseBodyMethodProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test  // SPR-13318
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\""));
}
 
Example 12
Source File: RequestResponseBodyMethodProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test  // SPR-12811
public void jacksonTypeInfoList() throws Exception {
	Method method = JacksonController.class.getMethod("handleTypeInfoList");
	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().handleTypeInfoList();
	processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request);

	String content = this.servletResponse.getContentAsString();
	assertTrue(content.contains("\"type\":\"foo\""));
	assertTrue(content.contains("\"type\":\"bar\""));
}
 
Example 13
Source File: RequestResponseBodyMethodProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test  // SPR-12149
public void jacksonJsonViewWithResponseEntityAndXmlMessageConverter() throws Exception {
	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()));

	Object returnValue = new JacksonController().handleResponseEntity();
	processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request);

	String content = this.servletResponse.getContentAsString();
	assertFalse(content.contains("<withView1>with</withView1>"));
	assertTrue(content.contains("<withView2>with</withView2>"));
	assertFalse(content.contains("<withoutView>without</withoutView>"));
}
 
Example 14
Source File: RequestResponseBodyMethodProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test  // SPR-12811
public void jacksonTypeInfoList() throws Exception {
	Method method = JacksonController.class.getMethod("handleTypeInfoList");
	HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
	MethodParameter methodReturnType = handlerMethod.getReturnType();

	List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
	converters.add(new MappingJackson2HttpMessageConverter());
	RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);

	Object returnValue = new JacksonController().handleTypeInfoList();
	processor.handleReturnValue(returnValue, methodReturnType, this.mavContainer, this.webRequest);

	String content = this.servletResponse.getContentAsString();
	assertTrue(content.contains("\"type\":\"foo\""));
	assertTrue(content.contains("\"type\":\"bar\""));
}
 
Example 15
Source File: RequestResponseBodyMethodProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test  // SPR-13318
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<HttpMessageConverter<?>>();
	converters.add(new MappingJackson2HttpMessageConverter());
	RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);

	Object returnValue = new JacksonController().handleSubType();
	processor.handleReturnValue(returnValue, methodReturnType, this.mavContainer, this.webRequest);

	String content = this.servletResponse.getContentAsString();
	assertTrue(content.contains("\"id\":123"));
	assertTrue(content.contains("\"name\":\"foo\""));
}
 
Example 16
Source File: RequestResponseBodyMethodProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void jacksonJsonViewWithResponseBodyAndJsonMessageConverter() throws Exception {
	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 MappingJackson2HttpMessageConverter());

	RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(
			converters, null, Collections.singletonList(new JsonViewResponseBodyAdvice()));

	Object returnValue = new JacksonController().handleResponseBody();
	processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request);

	String content = this.servletResponse.getContentAsString();
	assertFalse(content.contains("\"withView1\":\"with\""));
	assertTrue(content.contains("\"withView2\":\"with\""));
	assertFalse(content.contains("\"withoutView\":\"without\""));
}
 
Example 17
Source File: RequestResponseBodyMethodProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test  // SPR-12149
public void jacksonJsonViewWithResponseBodyAndXmlMessageConverter() throws Exception {
	Method method = JacksonController.class.getMethod("handleResponseBody");
	HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
	MethodParameter methodReturnType = handlerMethod.getReturnType();

	List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
	converters.add(new MappingJackson2XmlHttpMessageConverter());

	RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(
			converters, null, Collections.singletonList(new JsonViewResponseBodyAdvice()));

	Object returnValue = new JacksonController().handleResponseBody();
	processor.handleReturnValue(returnValue, methodReturnType, this.mavContainer, this.webRequest);

	String content = this.servletResponse.getContentAsString();
	assertFalse(content.contains("<withView1>with</withView1>"));
	assertTrue(content.contains("<withView2>with</withView2>"));
	assertFalse(content.contains("<withoutView>without</withoutView>"));
}
 
Example 18
Source File: RequestResponseBodyMethodProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test  // SPR-13631
public void defaultCharset() throws Exception {
	Method method = JacksonController.class.getMethod("defaultCharset");
	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().defaultCharset();
	processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request);

	assertEquals("UTF-8", this.servletResponse.getCharacterEncoding());
}
 
Example 19
Source File: DataRestResponseBuilder.java    From springdoc-openapi with Apache License 2.0 5 votes vote down vote up
/**
 * Build search response.
 *
 * @param operation the operation
 * @param handlerMethod the handler method
 * @param openAPI the open api
 * @param methodResourceMapping the method resource mapping
 * @param domainType the domain type
 * @param methodAttributes the method attributes
 */
public void buildSearchResponse(Operation operation, HandlerMethod handlerMethod, OpenAPI openAPI,
		MethodResourceMapping methodResourceMapping, Class<?> domainType, MethodAttributes methodAttributes) {
	MethodParameter methodParameterReturn = handlerMethod.getReturnType();
	ApiResponses apiResponses = new ApiResponses();
	ApiResponse apiResponse = new ApiResponse();
	Type returnType = findSearchReturnType(handlerMethod, methodResourceMapping, domainType);
	Content content = genericResponseBuilder.buildContent(openAPI.getComponents(), methodParameterReturn.getParameterAnnotations(), methodAttributes.getMethodProduces(), null, returnType);
	apiResponse.setContent(content);
	addResponse200(apiResponses, apiResponse);
	addResponse404(apiResponses);
	operation.setResponses(apiResponses);
}
 
Example 20
Source File: RequestResponseBodyMethodProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test  // SPR-13631
public void defaultCharset() throws Exception {
	Method method = JacksonController.class.getMethod("defaultCharset");
	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().defaultCharset();
	processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request);

	assertEquals("UTF-8", this.servletResponse.getCharacterEncoding());
}