?Author introduction: Hello everyone, I am Xiao Yang
Personal homepage: “Xiao Yang”‘s csdn blog
I hope everyone will support me and make progress together!
1, Introduction to unified data format return
Unified data return refers to standardizing and uniformly processing the returned data during interface development to ensure that the format and structure of the returned data are consistent.
A unified data return format can be achieved by using the @ControllerAdvice
annotation combined with the ResponseBodyAdvice
interface.
- The
@ControllerAdvice
annotation is used to define a global notification class, which can be used to define some global processing logic, such as exception handling, data binding, data formatting, etc. The class declared by this annotation can contain exception handling methods, pre-binding and post-binding interception methods, and other general processing logic. - The
ResponseBodyAdvice
interface is an interface provided by the Spring framework to intercept the response body. It can process the response data before the response body is returned to the client. By implementing this interface, data can be uniformly encapsulated, formatted or processed in the interceptor to achieve a unified data return format.
2. Implementation process of unified data format return
1, create a unified data return processing class
@ControllerAdvice public class ResponseAdvice implements ResponseBodyAdvice {<!-- --> }
2, rewriting two methods in ResponseBodyAdvice
2.1, supports method rewriting
Note: This method indicates whether to execute the subsequent beforeBodyWrite method. This method returns false by default, which means that no subsequent code operations, that is, data processing, will be performed. Only when true is returned, the subsequent beforeBodyWrite method will be executed.
2.2, beforeBodyWrite method rewriting
Note: This method implements the rewriting of the returned data format so that it returns a unified data format.
3, implementation code returned by unified data format
@ControllerAdvice public class ResponseAdvice implements ResponseBodyAdvice {<!-- --> @Override public boolean supports(MethodParameter returnType, Class converterType) {<!-- --> //Here you can determine whether the response body needs to be processed based on conditions // Returning true directly here means processing the response body of all requests. return true; } @Override public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {<!-- --> // The response body is processed here, and operations such as encapsulation and formatting can be performed. HashMap<String,Object> result = new HashMap<>(); result.put("code",200); result.put("msg",""); result.put("data",body); //Return the processed data return result; } }
4. Note on the implementation of unified data format return
During unified data processing, if the return type of your unified data processing method is String
, an error will be reported when returning.
The specific execution process when returning is:
- The method returns a
String
type. - Processing before performing unified data return: Convert
String
toHashMap
. - Convert
HashMap
intoapplication/json
string and return it to the front end.
According to the conclusion of the source code, the type of original data will be judged during the data conversion process, and the corresponding message converter will be selected to convert the data.
- If the type of the original data is
String
,StringHttpMessageConverter
will be used for type conversion. - If the original data is not
String
, other appropriateHttpMessageConverter
will be used for type conversion.
Since the unified data processing method returns the String
type, StringHttpMessageConverter
will be selected for conversion in the third step. However, StringHttpMessageConverter
cannot directly convert the HashMap
object into a string, thus causing an error.
Note: StringHttpMessageConverter
will only process string type data by default, so when using StringHttpMessageConverter
, you cannot directly convert the HashMap
object Convert directly to a string, while HttpMessageConverter
can convert HashMap
to a string.
To resolve this issue, you can do one of the following:
-
Remove
StringHttpMessageConverter
: In the Spring MVC configuration, you can consider removingStringHttpMessageConverter
so that the return value of typeString
will no longer be treated as The object that needs to be converted to JSON. This can be achieved by overriding theconfigureMessageConverters
method in the configuration class:@Configuration public class WebConfig implements WebMvcConfigurer {<!-- --> @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {<!-- --> converters.removeIf(converter -> converter instanceof StringHttpMessageConverter); } }
Note: This will use the default
HttpMessageConverter
to handle the return value without type conversion errors. -
Separate processing of
String
type return values: During the processing before unified data return, the type of the return value can be checked. If the type isString
, the string is returned directly to the front end without convertingHashMap
to JSON. This can be achieved through a custom unified data return processing class:@ControllerAdvice public class GlobalExceptionHandler {<!-- --> @ExceptionHandler(Exception.class) @ResponseBody public Object handleException(Exception ex) {<!-- --> if (ex instanceof YourCustomException) {<!-- --> //Handle custom exception logic return "Your custom error message"; } else if (ex instanceof AnotherException) {<!-- --> // Handle another exception logic return "Another error message"; } else {<!-- --> //Other exception handling logic return "Generic error message"; } } }
Note: In the above example, different processing is performed according to the data type, and the corresponding string is directly returned to the front end.
Through the above method, you can solve the problem of error reporting caused by the return type of the unified data processing method being String
, and perform special processing as needed. Please choose the appropriate solution based on your specific situation.
Conclusion
That’s all for this blog! If you have any other questions that you can’t solve yourself, you can leave a message in the comment area!
Finally, if you think this article is well written or has gained something, please friends, please move your little hands and give it three times in a row (like, comment?, collect), give me a lot of support one time! Your support is my biggest motivation. I will continue to update high-quality content in the future to help everyone and make progress together. See you next time!