add developer module
This commit is contained in:
+15
-9
@@ -7,19 +7,19 @@ import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpInputMessage;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.StringHttpMessageConverter;
|
||||
import org.springframework.http.server.ServerHttpRequest;
|
||||
import org.springframework.http.server.ServerHttpResponse;
|
||||
import org.springframework.util.MimeType;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
/**
|
||||
* The Class JsonStringBodyControllerAdvice.
|
||||
@@ -27,8 +27,6 @@ import com.google.gson.JsonPrimitive;
|
||||
@ControllerAdvice
|
||||
public class JsonStringBodyControllerAdvice implements RequestBodyAdvice, ResponseBodyAdvice<String> {
|
||||
|
||||
private Gson gson = new Gson();
|
||||
|
||||
/*
|
||||
* @see org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice#
|
||||
* supports(org.springframework.core.MethodParameter, java.lang.reflect.Type,
|
||||
@@ -49,6 +47,11 @@ public class JsonStringBodyControllerAdvice implements RequestBodyAdvice, Respon
|
||||
@Override
|
||||
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
|
||||
Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
|
||||
if (inputMessage.getHeaders() == null || inputMessage.getHeaders().getContentType() == null || !inputMessage
|
||||
.getHeaders().getContentType().equalsTypeAndSubtype(MimeType.valueOf("application/json"))) {
|
||||
throw new EntityResponseStatusException(HttpStatus.UNSUPPORTED_MEDIA_TYPE);
|
||||
}
|
||||
|
||||
return inputMessage;
|
||||
}
|
||||
|
||||
@@ -84,7 +87,7 @@ public class JsonStringBodyControllerAdvice implements RequestBodyAdvice, Respon
|
||||
*/
|
||||
@Override
|
||||
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
|
||||
return converterType == StringHttpMessageConverter.class;
|
||||
return returnType.getParameterType() == String.class;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -99,8 +102,11 @@ public class JsonStringBodyControllerAdvice implements RequestBodyAdvice, Respon
|
||||
public String beforeBodyWrite(String body, MethodParameter returnType, MediaType selectedContentType,
|
||||
Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
|
||||
ServerHttpResponse response) {
|
||||
response.getHeaders().set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
|
||||
return gson.toJson(new JsonPrimitive(body));
|
||||
if (body == null) {
|
||||
return "";
|
||||
}
|
||||
JsonElement json = JsonParser.parseString(body);
|
||||
return json.toString();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user