Skip to content

Commit

Permalink
自定义Spring Boot 内容协商
Browse files Browse the repository at this point in the history
  • Loading branch information
wuyouzhuguli committed Mar 13, 2019
1 parent 4a3a307 commit cb33779
Show file tree
Hide file tree
Showing 9 changed files with 334 additions and 0 deletions.
43 changes: 43 additions & 0 deletions 47.Spring-Boot-Content-Negotiation/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.example.demo.config;

import com.example.demo.converter.PropertiesHttpMessageConverter;
import com.example.demo.handler.PropertiesHandlerMethodReturnValueHandler;
import com.example.demo.resolver.PropertiesHandlerMethodArgumentResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
* @author MrBird
*/
@Configuration
public class WebConfigurer implements WebMvcConfigurer {


@Autowired
private RequestMappingHandlerAdapter requestMappingHandlerAdapter;

@PostConstruct
public void init() {
// 获取当前 RequestMappingHandlerAdapter 所有的 ArgumentResolver对象
List<HandlerMethodArgumentResolver> argumentResolvers = requestMappingHandlerAdapter.getArgumentResolvers();
List<HandlerMethodArgumentResolver> newArgumentResolvers = new ArrayList<>(argumentResolvers.size() + 1);
// 添加 PropertiesHandlerMethodArgumentResolver 到集合第一个位置
newArgumentResolvers.add(0, new PropertiesHandlerMethodArgumentResolver());
// 将原 ArgumentResolver 添加到集合中
newArgumentResolvers.addAll(argumentResolvers);
// 重新设置 ArgumentResolver对象集合
requestMappingHandlerAdapter.setArgumentResolvers(newArgumentResolvers);

// 获取当前 RequestMappingHandlerAdapter 所有的 returnValueHandlers对象
List<HandlerMethodReturnValueHandler> returnValueHandlers = requestMappingHandlerAdapter.getReturnValueHandlers();
List<HandlerMethodReturnValueHandler> newReturnValueHandlers = new ArrayList<>(returnValueHandlers.size() + 1);
// 添加 PropertiesHandlerMethodReturnValueHandler 到集合第一个位置
newReturnValueHandlers.add(0, new PropertiesHandlerMethodReturnValueHandler());
// 将原 returnValueHandlers 添加到集合中
newReturnValueHandlers.addAll(returnValueHandlers);
// 重新设置 ReturnValueHandlers对象集合
requestMappingHandlerAdapter.setReturnValueHandlers(newReturnValueHandlers);
}

public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
// converters.add(new PropertiesHttpMessageConverter());
// 指定顺序,这里为第一个
converters.add(0, new PropertiesHttpMessageConverter());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Properties;

/**
* @author MrBird
*/
// @RestController
@Controller
public class TestController {

@GetMapping(value = "test", consumes = "text/properties")
@ResponseBody
public Properties getUser(@RequestBody Properties properties) {
return properties;
}

@GetMapping(value = "test1", consumes = "text/properties")
// @ResponseBody
public Properties getUser1(Properties properties) {
return properties;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.example.demo.converter;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractGenericHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;

import java.io.*;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.Properties;

/**
* @author MrBird
*/
public class PropertiesHttpMessageConverter extends AbstractGenericHttpMessageConverter<Properties> {

public PropertiesHttpMessageConverter() {
super(new MediaType("text", "properties"));
}

@Override
protected void writeInternal(Properties properties, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
// 获取请求头
HttpHeaders headers = outputMessage.getHeaders();
// 获取 content-type
MediaType contentType = headers.getContentType();
// 获取编码
Charset charset = null;
if (contentType != null) {
charset = contentType.getCharset();
}

charset = charset == null ? Charset.forName("UTF-8") : charset;

// 获取请求体
OutputStream body = outputMessage.getBody();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(body, charset);

properties.store(outputStreamWriter, "Serialized by PropertiesHttpMessageConverter#writeInternal");
}

@Override
protected Properties readInternal(Class<? extends Properties> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
Properties properties = new Properties();
// 获取请求头
HttpHeaders headers = inputMessage.getHeaders();
// 获取 content-type
MediaType contentType = headers.getContentType();
// 获取编码
Charset charset = null;
if (contentType != null) {
charset = contentType.getCharset();
}

charset = charset == null ? Charset.forName("UTF-8") : charset;

// 获取请求体
InputStream body = inputMessage.getBody();
InputStreamReader inputStreamReader = new InputStreamReader(body, charset);

properties.load(inputStreamReader);
return properties;
}

@Override
public Properties read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
return readInternal(null, inputMessage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.example.demo.handler;

import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.Properties;

/**
* @author MrBird
*/
public class PropertiesHandlerMethodReturnValueHandler implements HandlerMethodReturnValueHandler {

@Override
public boolean supportsReturnType(MethodParameter returnType) {
return Properties.class.equals(returnType.getMethod().getReturnType());
}

@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
Properties properties = (Properties) returnValue;

ServletWebRequest servletWebRequest = (ServletWebRequest) webRequest;

HttpServletResponse response = servletWebRequest.getResponse();
ServletServerHttpResponse servletServerHttpResponse = new ServletServerHttpResponse(response);

// 获取请求头
HttpHeaders headers = servletServerHttpResponse.getHeaders();

MediaType contentType = headers.getContentType();
// 获取编码
Charset charset = null;
if (contentType != null) {
charset = contentType.getCharset();
}

charset = charset == null ? Charset.forName("UTF-8") : charset;

// 获取请求体
OutputStream body = servletServerHttpResponse.getBody();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(body, charset);

properties.store(outputStreamWriter, "Serialized by PropertiesHandlerMethodReturnValueHandler#handleReturnValue");

// 告诉 Spring MVC 请求已经处理完毕
mavContainer.setRequestHandled(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.example.demo.resolver;

import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Properties;

/**
* @author MrBird
*/
public class PropertiesHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
@Override
public boolean supportsParameter(MethodParameter parameter) {
return Properties.class.equals(parameter.getParameterType());
}

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
ServletWebRequest servletWebRequest = (ServletWebRequest) webRequest;
HttpServletRequest request = servletWebRequest.getRequest();
String contentType = request.getHeader("Content-Type");

MediaType mediaType = MediaType.parseMediaType(contentType);
// 获取编码
Charset charset = mediaType.getCharset() == null ? Charset.forName("UTF-8") : mediaType.getCharset();
// 获取输入流
InputStream inputStream = request.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, charset);

// 输入流转换为 Properties
Properties properties = new Properties();
properties.load(inputStreamReader);
return properties;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

@Test
public void contextLoads() {
}

}

0 comments on commit cb33779

Please sign in to comment.