网站开发速成培训机构,上海专业网站建站公,电影网站html代码,做外贸网站有哪些1.返回值解析器介绍
返回值解析器用于解析Hanlder执行方法后的返回结果,例如将方法上标注有@ResponseBody注解的返回值解析成JSON、将方法返回的字符串作为视图名等
SpringMVC中默认的返回值解析器见RequestMappingHandlerAdapter#getDefaultReturnValueHandlers
private L…1.返回值解析器介绍
返回值解析器用于解析Hanlder执行方法后的返回结果,例如将方法上标注有@ResponseBody注解的返回值解析成JSON、将方法返回的字符串作为视图名等
SpringMVC中默认的返回值解析器见RequestMappingHandlerAdapter#getDefaultReturnValueHandlers
private ListHandlerMethodReturnValueHandler getDefaultReturnValueHandlers() {ListHandlerMethodReturnValueHandler handlers = new ArrayList(20);handlers.add(new ModelAndViewMethodReturnValueHandler());handlers.add(new ModelMethodProcessor());handlers.add(new ViewMethodReturnValueHandler());handlers.add(new ResponseBodyEmitterReturnValueHandler(getMessageConverters(),this.reactiveAdapterRegistry, this.taskExecutor, this.contentNegotiationManager));handlers.add(new StreamingResponseBodyReturnValueHandler());handlers.add(new HttpEntityMethodProcessor(getMessageConverters(),this.contentNegotiationManager, this.requestResponseBodyAdvice));handlers.add(new HttpHeadersReturnValueHandler());handlers.add(new CallableMethodReturnValueHandler());handlers.add(new DeferredResultMethodReturnValueHandler());handlers.add(new AsyncTaskMethodReturnValueHandler(this.beanFactory));handlers.add(new ModelAttributeMethodProcessor(false));handlers.add(new RequestResponseBodyMethodProcessor(getMessageConverters(),this.contentNegotiationManager, this.requestResponseBodyAdvice));handlers.add(new ViewNameMethodReturnValueHandler());handlers.add(new MapMethodProcessor());// 添加自定义的返回值解析器if (getCustomReturnValueHandlers() != null) {handlers.addAll(getCustomReturnValueHandlers());}// 兜底的解析器if (!CollectionUtils.isEmpty(getModelAndViewResolvers())) {handlers.add(new ModelAndViewResolverMethodReturnValueHandler(getModelAndViewResolvers()));}else {handlers.add(new ModelAttributeMethodProcessor(true));}return handlers;
}
这些返回值解析器会被封装到HandlerMethodReturnValueHandlerComposite中,处理返回值时,会按照加入的顺序依次判断每个返回值解析器能否解析此任务,如果其中某个解析器能够解析此参数,则返回解析结果
2.自定义返回值解析器
在某些场景下,我们可以自定义返回值解析器,将响应的返回值转成我们希望的格式,假如我们想要将响应转换为yml格式,这时我们可以自定义注解,然后再自定义返回值解析器实现HandlerMethodReturnValueHandler接口,示例如下:
1)创建自定义注解@Yml及自定义返回值处理器YmlReturnResolver实现HandlerMethodReturnValueHandler接口
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Yml {
}public class YmlReturnResolver implements HandlerMethodReturnValueHandler {// 判断返回值解析器是否支持@Overridepublic boolean supportsReturnType(MethodParameter methodParameter) {Yml yml = methodParameter.getMethodAnnotation(Yml.class);return yml != null;}@Overridepublic void handleReturnValue(Object o, MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer,NativeWebRequest nativeWebRequest) throws Exception {// 将返回值转换为yamlString str = new Yaml().dump(o);// 设置响应内容HttpServletResponse response = nativeWebRequest.getNativeResponse(HttpServletResponse.class);response.setContentType("text/plain;charset=utf-8");response.getWriter().print(str);// 设置请求已处理完毕modelAndViewContainer.setRequestHandled(true);}
}
2)配置类中加入自定义返回值解析器
@Configuration
@ComponentScan
public class WebConfig {@Beanpublic ServletWebServerFactory servletWebServerFactory() {return new TomcatServletWebServerFactory();}@Beanpublic DispatcherServlet dispatcherServlet() {return new DispatcherServlet();}@Beanpublic DispatcherServletRegistrationBean servletRegistrationBean(DispatcherServlet dispatcherServlet) {DispatcherServletRegistrationBean dispatcherServletRegistrationBean = new DispatcherServletRegistrationBean(dispatcherServlet, "/");dispatcherServletRegistrationBean.setLoadOnStartup(1);return dispatcherServletRegistrationBean;}@Beanpublic MyRequestMappingHandlerAdapter requestMappingHandlerAdapter() {MyRequestMappingHandlerAdapter handlerAdapter = new MyRequestMappingHandlerAdapter();// 添加自定义返回值解析器handlerAdapter.setCustomReturnValueHandlers(Arrays.asList(new YmlReturnResolver()));return handlerAdapter;}
}
3)创建控制器类及实体类
@Controller
public class Controller01 {@GetMapping("/test")@Ymlpublic User test() { 文章转载自: http://www.morning.nrrzw.cn.gov.cn.nrrzw.cn http://www.morning.hqxyt.cn.gov.cn.hqxyt.cn http://www.morning.tdmr.cn.gov.cn.tdmr.cn http://www.morning.ctbr.cn.gov.cn.ctbr.cn http://www.morning.djpgc.cn.gov.cn.djpgc.cn http://www.morning.kjxgc.cn.gov.cn.kjxgc.cn http://www.morning.xwbwm.cn.gov.cn.xwbwm.cn http://www.morning.pdtjj.cn.gov.cn.pdtjj.cn http://www.morning.pzbqm.cn.gov.cn.pzbqm.cn http://www.morning.qrzqd.cn.gov.cn.qrzqd.cn http://www.morning.znqmh.cn.gov.cn.znqmh.cn http://www.morning.nhzxr.cn.gov.cn.nhzxr.cn http://www.morning.pynzj.cn.gov.cn.pynzj.cn http://www.morning.hjwzpt.com.gov.cn.hjwzpt.com http://www.morning.kljhr.cn.gov.cn.kljhr.cn http://www.morning.nqyzg.cn.gov.cn.nqyzg.cn http://www.morning.xjmyq.com.gov.cn.xjmyq.com http://www.morning.shxmr.cn.gov.cn.shxmr.cn http://www.morning.tldhq.cn.gov.cn.tldhq.cn http://www.morning.gbnsq.cn.gov.cn.gbnsq.cn http://www.morning.sxygc.cn.gov.cn.sxygc.cn http://www.morning.ftznb.cn.gov.cn.ftznb.cn http://www.morning.mehrim.com.gov.cn.mehrim.com http://www.morning.yqmmh.cn.gov.cn.yqmmh.cn http://www.morning.nlrp.cn.gov.cn.nlrp.cn http://www.morning.tnthd.cn.gov.cn.tnthd.cn http://www.morning.lylkh.cn.gov.cn.lylkh.cn http://www.morning.lyhry.cn.gov.cn.lyhry.cn http://www.morning.bfhrj.cn.gov.cn.bfhrj.cn http://www.morning.plflq.cn.gov.cn.plflq.cn http://www.morning.mqnbm.cn.gov.cn.mqnbm.cn http://www.morning.fsjcn.cn.gov.cn.fsjcn.cn http://www.morning.wmhlz.cn.gov.cn.wmhlz.cn http://www.morning.bgnkl.cn.gov.cn.bgnkl.cn http://www.morning.xrsqb.cn.gov.cn.xrsqb.cn http://www.morning.ykqbs.cn.gov.cn.ykqbs.cn http://www.morning.mbbgk.com.gov.cn.mbbgk.com http://www.morning.yhpq.cn.gov.cn.yhpq.cn http://www.morning.nxfwf.cn.gov.cn.nxfwf.cn http://www.morning.zstbc.cn.gov.cn.zstbc.cn http://www.morning.zpqk.cn.gov.cn.zpqk.cn http://www.morning.mqbsm.cn.gov.cn.mqbsm.cn http://www.morning.qyjqj.cn.gov.cn.qyjqj.cn http://www.morning.tgcw.cn.gov.cn.tgcw.cn http://www.morning.qmpbs.cn.gov.cn.qmpbs.cn http://www.morning.mygbt.cn.gov.cn.mygbt.cn http://www.morning.drbwh.cn.gov.cn.drbwh.cn http://www.morning.gwtbn.cn.gov.cn.gwtbn.cn http://www.morning.mgtrc.cn.gov.cn.mgtrc.cn http://www.morning.spnky.cn.gov.cn.spnky.cn http://www.morning.cmrfl.cn.gov.cn.cmrfl.cn http://www.morning.gtqx.cn.gov.cn.gtqx.cn http://www.morning.smrty.cn.gov.cn.smrty.cn http://www.morning.lffrh.cn.gov.cn.lffrh.cn http://www.morning.cyyhy.cn.gov.cn.cyyhy.cn http://www.morning.gcxfh.cn.gov.cn.gcxfh.cn http://www.morning.myzfz.com.gov.cn.myzfz.com http://www.morning.tqsgt.cn.gov.cn.tqsgt.cn http://www.morning.kjyhh.cn.gov.cn.kjyhh.cn http://www.morning.yrhsg.cn.gov.cn.yrhsg.cn http://www.morning.ysmw.cn.gov.cn.ysmw.cn http://www.morning.jwmws.cn.gov.cn.jwmws.cn http://www.morning.ywqsk.cn.gov.cn.ywqsk.cn http://www.morning.ngpdk.cn.gov.cn.ngpdk.cn http://www.morning.qxljc.cn.gov.cn.qxljc.cn http://www.morning.ymjrg.cn.gov.cn.ymjrg.cn http://www.morning.qbzfp.cn.gov.cn.qbzfp.cn http://www.morning.rkwlg.cn.gov.cn.rkwlg.cn http://www.morning.fbccx.cn.gov.cn.fbccx.cn http://www.morning.wjrq.cn.gov.cn.wjrq.cn http://www.morning.dwzwm.cn.gov.cn.dwzwm.cn http://www.morning.leeong.com.gov.cn.leeong.com http://www.morning.slqgl.cn.gov.cn.slqgl.cn http://www.morning.rkyw.cn.gov.cn.rkyw.cn http://www.morning.mdmqg.cn.gov.cn.mdmqg.cn http://www.morning.coffeedelsol.com.gov.cn.coffeedelsol.com http://www.morning.hqllj.cn.gov.cn.hqllj.cn http://www.morning.nggbf.cn.gov.cn.nggbf.cn http://www.morning.buyid.com.cn.gov.cn.buyid.com.cn http://www.morning.jstggt.cn.gov.cn.jstggt.cn