国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

詳解SpringBoot定制@ResponseBody注解返回的Json格式

瀏覽:6日期:2023-04-11 09:38:29

1、引言

在SpringMVC的使用中,后端與前端的交互一般是使用Json格式進行數(shù)據(jù)傳輸,SpringMVC的@ResponseBody注解可以很好的幫助我們進行轉(zhuǎn)換,但是后端返回數(shù)據(jù)給前端往往都有約定固定的格式,這時候我們在后端返回的時候都要組拼成固定的格式,每次重復(fù)的操作非常麻煩。

2、SpringMVC對@ResponseBody的處理

SpringMVC處理@ResponseBody注解聲明的Controller是使用默認(rèn)的.RequestResponseBodyMethodProcessor類來實現(xiàn),RequestResponseBodyMethodProcessor類實現(xiàn)了HandlerMethodReturnValueHandler接口并實現(xiàn)了接口中的supportsReturnType()和handleReturnValue()方法。

/* * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the 'License'); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an 'AS IS' BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.springframework.web.method.support;import org.springframework.core.MethodParameter;import org.springframework.lang.Nullable;import org.springframework.web.context.request.NativeWebRequest;/** * Strategy interface to handle the value returned from the invocation of a * handler method . * * @author Arjen Poutsma * @since 3.1 * @see HandlerMethodArgumentResolver */public interface HandlerMethodReturnValueHandler { /** * Whether the given {@linkplain MethodParameter method return type} is * supported by this handler. * @param returnType the method return type to check * @return {@code true} if this handler supports the supplied return type; * {@code false} otherwise */ boolean supportsReturnType(MethodParameter returnType); /** * Handle the given return value by adding attributes to the model and * setting a view or setting the * {@link ModelAndViewContainer#setRequestHandled} flag to {@code true} * to indicate the response has been handled directly. * @param returnValue the value returned from the handler method * @param returnType the type of the return value. This type must have * previously been passed to {@link #supportsReturnType} which must * have returned {@code true}. * @param mavContainer the ModelAndViewContainer for the current request * @param webRequest the current request * @throws Exception if the return value handling results in an error */ void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception;}

3、實現(xiàn)思路

知道@ResponseBody是由RequestResponseBodyMethodProcessor進行處理的,這時候我們可以自己定義一個處理返回數(shù)據(jù)的Handler來實現(xiàn)我們的定制化Json格式數(shù)據(jù)返回,但是如果直接把我們定制的Handler加入到SpringMVC的ReturnValueHandlers中,因為我們定制的Handler在RequestResponseBodyMethodProcessor之后,所以我們定制的Handler還是不會生效,這時候我們可以想辦法把RequestResponseBodyMethodProcessor替換成我們定制的Handler。

4、代碼實現(xiàn)

4.1、定制Json返回格式實體

package com.autumn.template;import lombok.AllArgsConstructor;import lombok.Getter;import lombok.NoArgsConstructor;import lombok.Setter;import lombok.experimental.Accessors;/** * JSON信息交互對象模板 * @Author Autumn、 * @Date 2019/4/8 23:46 * @Description */@Setter@Getter@AllArgsConstructor@NoArgsConstructor@Accessors(chain = true)public class Result implements BaseBean { ......(這里只展示一些必要字段) /** 響應(yīng)碼 */ private Integer code; /** 響應(yīng)信息 */ private String message; /** 數(shù)據(jù) */ private Object data; /** 請求地址 */ private String url; ......}

4.2、定義定制Json返回格式Handler

package com.autumn.component.handler;import com.autumn.template.Result;import org.springframework.core.MethodParameter;import org.springframework.lang.Nullable;import org.springframework.web.context.request.NativeWebRequest;import org.springframework.web.method.support.HandlerMethodReturnValueHandler;import org.springframework.web.method.support.ModelAndViewContainer;/** * 統(tǒng)一處理ResponseBody數(shù)據(jù)格式 * @Author: Autumn、 * @Date: 2019/4/24 23:59 * @Description: **/public class ResultWarpReturnValueHandler implements HandlerMethodReturnValueHandler { private final HandlerMethodReturnValueHandler delegate; /** 委托 */ public ResultWarpReturnValueHandler(HandlerMethodReturnValueHandler delegate) { this.delegate = delegate; } /** * 判斷返回類型是否需要轉(zhuǎn)成字符串返回 * @param returnType 方法返回類型 * @return 需要轉(zhuǎn)換返回true,否則返回false */ @Override public boolean supportsReturnType(MethodParameter returnType) { return delegate.supportsReturnType(returnType); } /** * 返回值轉(zhuǎn)換 */ @Override public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception { // 委托SpringMVC默認(rèn)的RequestResponseBodyMethodProcessor進行序列化 delegate.handleReturnValue(returnValue instanceof Result ? returnValue : Result.succeed(returnValue), returnType, mavContainer, webRequest); }}

4.3、替換默認(rèn)的RequestResponseBodyMethodProcessor

package com.autumn.config;import com.autumn.component.handler.ResultWarpReturnValueHandler;import org.springframework.beans.factory.InitializingBean;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cache.annotation.EnableCaching;import org.springframework.context.annotation.Configuration;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 org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;import java.util.ArrayList;import java.util.List;import lombok.extern.slf4j.Slf4j;/** * 替換默認(rèn)的RequestResponseBodyMethodProcessor * @Author Autumn、 * @Date 2019/4/8 23:46 * @Description */@Slf4j@Configuration@EnableCachingpublic class ApplicationContext implements WebMvcConfigurer, InitializingBean { @Autowired(required = false) private RequestMappingHandlerAdapter adapter; @Override public void afterPropertiesSet() throws Exception { // 獲取SpringMvc的ReturnValueHandlers List<HandlerMethodReturnValueHandler> returnValueHandlers = adapter.getReturnValueHandlers(); // 新建一個List來保存替換后的Handler的List List<HandlerMethodReturnValueHandler> handlers = new ArrayList<>(returnValueHandlers); // 循環(huán)遍歷找出RequestResponseBodyMethodProcessor for (HandlerMethodReturnValueHandler handler : handlers) { if (handler instanceof RequestResponseBodyMethodProcessor) { // 創(chuàng)建定制的Json格式處理HandlerResultWarpReturnValueHandler decorator = new ResultWarpReturnValueHandler(handler);// 使用定制的Json格式處理Handler替換原有的RequestResponseBodyMethodProcessorint index = handlers.indexOf(handler);handlers.set(index, decorator);break; } } // 重新設(shè)置SpringMVC的ReturnValueHandlers adapter.setReturnValueHandlers(handlers); }}

5、總結(jié)

至此完成了定制@ResponseBody注解返回的Json格式,在Controller中返回任何的字符串都可以定制成為我們想要的Json格式。此外SpringMVC還提供了非常多的Handler接口來進行Controller的增強,可以使用此思路對參數(shù)等進行定制化。

到此這篇關(guān)于詳解SpringBoot定制@ResponseBody注解返回的Json格式的文章就介紹到這了,更多相關(guān)SpringBoot @ResponseBody返回Json內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 日韩欧美不卡一区二区三区 | 欧美在线观看高清一二三区 | 看欧美的一级毛片 | 99免费在线观看视频 | 国产成人91一区二区三区 | 99j久久精品久久久久久 | 视频一区中文字幕 | 伊人精品视频 | 精品一区二区三区中文 | 成年视频在线 | 曰韩一级毛片 | 一本色道久久88亚洲精品综合 | 毛片在线全部免费观看 | 一级一片免费视频播放 | 国产伦精品一区二区三区网站 | 欧美大陆日韩 | 自怕偷自怕亚洲精品 | 青青自拍视频一区二区三区 | 在线观看国产一区二三区 | 草草影院永久在线观看 | 国产精品久久久久久免费播放 | 欧美激情视频一级视频一级毛片 | 一级成人毛片免费观看 | 一区二区三区国产美女在线播放 | 国产一区自拍视频 | 久久久久久亚洲精品中文字幕 | 夜间福利在线观看 | 欧美日韩国产综合一区二区三区 | 日韩一区二区在线免费观看 | 国产美女精品在线 | 精品国产自在在线在线观看 | 成年女人看片免费视频频 | 国亚洲欧美日韩精品 | 中国一级做a爰片久久毛片 中日韩欧美一级毛片 | 2019偷偷狠狠的日日 | 一级一片一a一片 | 国产精品秒播无毒不卡 | 久久99精品久久久久久秒播 | 呦视频在线一区二区三区 | 欧美精品一区二区三区免费 | 1024色淫免费视频 |