-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
inkroom
committed
Aug 30, 2019
1 parent
74e7632
commit 3eb6e25
Showing
8 changed files
with
228 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.inkbox.boot.demo.config; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.HttpOutputMessage; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.converter.HttpMessageConverter; | ||
import org.springframework.http.converter.HttpMessageNotWritableException; | ||
import org.springframework.http.converter.StringHttpMessageConverter; | ||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; | ||
import org.springframework.messaging.converter.MappingJackson2MessageConverter; | ||
import org.springframework.util.StreamUtils; | ||
import org.springframework.web.servlet.config.annotation.EnableWebMvc; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Type; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
/** | ||
* 解决返回字符串时再次被json转换,最后多出双引号问题 | ||
*/ | ||
@Configuration | ||
public class MvcConfig implements WebMvcConfigurer { | ||
|
||
private Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
@Autowired | ||
private MappingJackson2HttpMessageConverter converter; | ||
|
||
@Override | ||
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { | ||
// MappingJackson2HttpMessageConverter converter = mappingJackson2HttpMessageConverter(); | ||
converter.setSupportedMediaTypes(new LinkedList<MediaType>() {{ | ||
add(MediaType.TEXT_HTML); | ||
add(MediaType.APPLICATION_JSON_UTF8); | ||
}}); | ||
converters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8)); | ||
converters.add(converter); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/com/inkbox/boot/demo/converter/Jackson2HttpMessageConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.inkbox.boot.demo.converter; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.http.HttpOutputMessage; | ||
import org.springframework.http.converter.HttpMessageNotWritableException; | ||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.StreamUtils; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Type; | ||
import java.nio.charset.Charset; | ||
|
||
@Component | ||
public class Jackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter { | ||
|
||
private Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
@Override | ||
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { | ||
if (object instanceof String) { | ||
//绕开实际上返回的String类型,不序列化 | ||
Charset charset = this.getDefaultCharset(); | ||
StreamUtils.copy((String) object, charset, outputMessage.getBody()); | ||
} else { | ||
super.writeInternal(object, type, outputMessage); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.inkbox.boot.demo.dto; | ||
|
||
|
||
public class Position { | ||
|
||
private int x; | ||
private int y; | ||
|
||
public Position() { | ||
} | ||
|
||
public Position(int x, int y) { | ||
this.x = x; | ||
this.y = y; | ||
} | ||
|
||
public int getX() { | ||
return x; | ||
} | ||
|
||
public void setX(int x) { | ||
this.x = x; | ||
} | ||
|
||
public int getY() { | ||
return y; | ||
} | ||
|
||
public void setY(int y) { | ||
this.y = y; | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
src/main/java/com/inkbox/boot/demo/handler/JsonpAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.inkbox.boot.demo.handler; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.inkbox.boot.demo.controllers.IndexController; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.server.ServerHttpRequest; | ||
import org.springframework.http.server.ServerHttpResponse; | ||
import org.springframework.http.server.ServletServerHttpRequest; | ||
import org.springframework.util.StringUtils; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
/** | ||
* 处理controller返回值,对于有callback值的使用jsonp格式,其余不处理 | ||
*/ | ||
@RestControllerAdvice(basePackageClasses = IndexController.class) | ||
public class JsonpAdvice implements ResponseBodyAdvice { | ||
|
||
private Logger logger = LoggerFactory.getLogger(getClass()); | ||
@Autowired | ||
private ObjectMapper mapper; | ||
|
||
//jquery默认是callback,其余jsonp库可能不一样 | ||
private final String callBackKey = "callback"; | ||
|
||
@Override | ||
public boolean supports(MethodParameter methodParameter, Class aClass) { | ||
logger.debug("返回的class={}", aClass); | ||
return true; | ||
} | ||
|
||
/** | ||
* 在此处对返回值进行处理,需要特别注意如果是非String类型,会被Json序列化,从而添加了双引号,解决办法见 | ||
* | ||
* @param body 返回值 | ||
* @param methodParameter 方法参数 | ||
* @param mediaType 当前contentType,非String类型为json | ||
* @param aClass convert的class | ||
* @param serverHttpRequest request,暂时支持是ServletServerHttpRequest类型,其余类型将会原样返回 | ||
* @param serverHttpResponse response | ||
* @return 如果body是String类型,加上方法头后返回,如果是其他类型,序列化后返回 | ||
* @see com.inkbox.boot.demo.converter.Jackson2HttpMessageConverter | ||
*/ | ||
@Override | ||
public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) { | ||
|
||
if (body == null) | ||
return null; | ||
//TODO 2019/8/30 如果返回String类型,media是plain,否则是json,将会经过json序列化,在下方返回纯字符串之后依然会被序列化,就会添上多余的双引号 | ||
logger.debug("body={},request={},response={},media={}", body, serverHttpRequest, serverHttpResponse, mediaType.getSubtype()); | ||
|
||
|
||
if (serverHttpRequest instanceof ServletServerHttpRequest) { | ||
HttpServletRequest request = ((ServletServerHttpRequest) serverHttpRequest).getServletRequest(); | ||
|
||
String callback = request.getParameter(callBackKey); | ||
|
||
if (!StringUtils.isEmpty(callback)) { | ||
//使用了jsonp | ||
if (body instanceof String) { | ||
return callback + "(\"" + body + "\")"; | ||
} else { | ||
try { | ||
String res = mapper.writeValueAsString(body); | ||
logger.debug("转化后的返回值={},{}", res, callback + "(" + res + ")"); | ||
|
||
return callback + "(" + res + ")"; | ||
} catch (JsonProcessingException e) { | ||
logger.warn("【jsonp支持】数据body序列化失败", e); | ||
return body; | ||
} | ||
} | ||
} | ||
} else { | ||
logger.warn("【jsonp支持】不支持的request class ={}", serverHttpRequest.getClass()); | ||
} | ||
return body; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters