当前位置: 首页 > news >正文

珠海新闻网seo网站优化方

珠海新闻网,seo网站优化方,网站做推广企业,网站设计与制作合同标签:Rest.拦截器.swagger.测试; 一、简介 基于web包的依赖,SpringBoot可以快速启动一个web容器,简化项目的开发; 在web开发中又涉及如下几个功能点: 拦截器:可以让接口被访问之前,将请求拦截…

标签:Rest.拦截器.swagger.测试;

一、简介

基于web包的依赖,SpringBoot可以快速启动一个web容器,简化项目的开发;

web开发中又涉及如下几个功能点:

拦截器:可以让接口被访问之前,将请求拦截到,通过对请求的识别和校验,判断请求是否允许通过;

页面交互:对于服务端的开发来说,需要具备简单的页面开发能力,解决部分场景的需求;

Swagger接口:通过简单的配置,快速生成接口的描述,并且提供对接口的测试能力;

Junit测试:通过编写代码的方式对接口进行测试,从而完成对接口的检查和验证,并且可以不入侵原代码结构;

二、工程搭建

1、工程结构

2、依赖管理

<!-- 基础框架组件 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>${spring-boot.version}</version>
</dependency>
<!-- 接口文档组件 -->
<dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>${springdoc.version}</version>
</dependency>
<!-- 前端页面组件 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId><version>${spring-boot.version}</version>
</dependency>
<!-- 单元测试组件 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><version>${spring-boot.version}</version><exclusions><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId></exclusion></exclusions>
</dependency>
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version>
</dependency>

三、Web开发

1、接口开发

编写四个简单常规的接口,从对资源操作的角度,也就是常说的:增Post、删Delete、改Put、查Get,并且使用了swagger注解,可以快速生成接口文档;

@RestController
@Tag(name = "Rest接口")
public class RestWeb {@Operation(summary = "Get接口")@GetMapping("rest/get/{id}")public String restGet(@PathVariable Integer id) {return "OK:"+id;}@Operation(summary = "Post接口")@PostMapping("/rest/post")public String restPost(@RequestBody ParamBO param){return "OK:"+param.getName();}@Operation(summary = "Put接口")@PutMapping("/rest/put")public String restPut(@RequestBody ParamBO param){return "OK:"+param.getId();}@Operation(summary = "Delete接口")@DeleteMapping("/rest/delete/{id}")public String restDelete(@PathVariable Integer id){return "OK:"+id;}
}

2、页面交互

对于服务端开发来说,在部分场景下是需要进行简单的页面开发的,比如通过页面渲染再去生成文件,或者直接通过页面填充邮件内容等;

数据接口

@Controller
public class PageWeb {@RequestMapping("/page/view")public ModelAndView pageView (HttpServletRequest request){ModelAndView modelAndView = new ModelAndView() ;// 普通参数modelAndView.addObject("name", "cicada");modelAndView.addObject("time", "2023-07-12");// 对象模型modelAndView.addObject("page", new PageBO(7,"页面数据模型"));// List集合List<PageBO> pageList = new ArrayList<>() ;pageList.add(new PageBO(1,"第一页"));pageList.add(new PageBO(2,"第二页"));modelAndView.addObject("pageList", pageList);// Array数组PageBO[] pageArr = new PageBO[]{new PageBO(6,"第六页"),new PageBO(7,"第七页")} ;modelAndView.addObject("pageArr", pageArr);modelAndView.setViewName("/page-view");return modelAndView ;}
}

页面解析:分别解析了普通参数,实体对象,集合容器,数组容器等几种数据模型;

<div style="text-align: center"><hr/><h5>普通参数解析</h5>姓名:<span th:text="${name}"></span>时间:<span th:text="${time}"></span><hr/><h5>对象模型解析</h5>整形:<span th:text="${page.getKey()}"></span>字符:<span th:text="${page.getValue()}"></span><hr/><h5>集合容器解析</h5><table style="margin:0 auto;width: 200px"><tr><th>Key</th><th>Value</th></tr><tr th:each="page:${pageList}"><td th:text="${page.getKey()}"></td><td th:text="${page.getValue()}"></td></tr></table><hr/><h5>数组容器解析</h5><table style="margin:0 auto;width: 200px"><tr><th>Key</th><th>Value</th></tr><tr th:each="page:${pageArr}"><td th:text="${page.getKey()}"></td><td th:text="${page.getValue()}"></td></tr></table><hr/>
</div>

效果图展示

四、拦截器

1、拦截器定义

通过实现HandlerInterceptor接口,完成对两个拦截器的自定义,请求在访问服务时,必须通过两个拦截器的校验;

/*** 拦截器一*/
public class HeadInterceptor implements HandlerInterceptor {private static final Logger log  = LoggerFactory.getLogger(HeadInterceptor.class);@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws Exception {log.info("HeadInterceptor:preHandle");Iterator<String> headNames = request.getHeaderNames().asIterator();log.info("request-header");while (headNames.hasNext()){String headName = headNames.next();String headValue = request.getHeader(headName);System.out.println(headName+":"+headValue);}// 放开拦截return true;}@Overridepublic void postHandle(HttpServletRequest request,HttpServletResponse response,Object handler, ModelAndView modelAndView) throws Exception {log.info("HeadInterceptor:postHandle");}@Overridepublic void afterCompletion(HttpServletRequest request,HttpServletResponse response,Object handler, Exception e) throws Exception {log.info("HeadInterceptor:afterCompletion");}
}/*** 拦截器二*/
public class BodyInterceptor implements HandlerInterceptor {private static final Logger log  = LoggerFactory.getLogger(BodyInterceptor.class);@Overridepublic boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handler) throws Exception {log.info("BodyInterceptor:preHandle");Iterator<String> paramNames = request.getParameterNames().asIterator();log.info("request-param");while (paramNames.hasNext()){String paramName = paramNames.next();String paramValue = request.getParameter(paramName);System.out.println(paramName+":"+paramValue);}// 放开拦截return true;}@Overridepublic void postHandle(HttpServletRequest request,HttpServletResponse response,Object handler, ModelAndView modelAndView) throws Exception {log.info("BodyInterceptor:postHandle");}@Overridepublic void afterCompletion(HttpServletRequest request,HttpServletResponse response,Object handler, Exception e) throws Exception {log.info("BodyInterceptor:afterCompletion");}
}

2、拦截器配置

自定义拦截器之后,还需要添加到web工程的配置文件中,可以通过实现WebMvcConfigurer接口,完成自定义的配置添加;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {/*** 添加自定义拦截器*/@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new HeadInterceptor()).addPathPatterns("/**");registry.addInterceptor(new BodyInterceptor()).addPathPatterns("/**");}
}

五、测试工具

1、Swagger接口

添加上述的springdoc依赖之后,还可以在配置文件中简单定义一些信息,访问IP:端口/swagger-ui/index.html即可;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {/*** 接口文档配置*/@Beanpublic OpenAPI openAPI() {return new OpenAPI().info(new Info().title("【boot-web】").description("Rest接口文档-2023-07-11").version("1.0.0"));}
}

2、Junit测试

在个人的习惯上,Swagger接口文档更偏向在前后端对接的时候使用,而Junit单元测试更符合开发的时候使用,这里是对RestWeb中的接口进行测试;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class RestWebTest {@Autowiredprivate MockMvc mockMvc;@Testpublic void testGet () throws Exception {// GET接口测试MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/rest/get/1")).andReturn();printMvcResult(mvcResult);}@Testpublic void testPost () throws Exception {// 参数模型JsonMapper jsonMapper = new JsonMapper();ParamBO param = new ParamBO(null,"单元测试",new Date()) ;String paramJson = jsonMapper.writeValueAsString(param) ;// Post接口测试MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/rest/post").contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).content(paramJson)).andReturn();printMvcResult(mvcResult);}@Testpublic void testPut () throws Exception {// 参数模型JsonMapper jsonMapper = new JsonMapper();ParamBO param = new ParamBO(7,"Junit组件",new Date()) ;String paramJson = jsonMapper.writeValueAsString(param) ;// Put接口测试MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.put("/rest/put").contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).content(paramJson)).andReturn();printMvcResult(mvcResult);}@Testpublic void testDelete () throws Exception {// Delete接口测试MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.delete("/rest/delete/2")).andReturn();printMvcResult(mvcResult);}/*** 打印【MvcResult】信息*/private void printMvcResult (MvcResult mvcResult) throws Exception {System.out.println("请求-URI【"+mvcResult.getRequest().getRequestURI()+"】");System.out.println("响应-status【"+mvcResult.getResponse().getStatus()+"】");System.out.println("响应-content【"+mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8)+"】");}
}

六、参考源码

文档仓库:
https://gitee.com/cicadasmile/butte-java-note源码仓库:
https://gitee.com/cicadasmile/butte-spring-parent

文章转载自:
http://dinncopaleosol.tpps.cn
http://dinncoclericalize.tpps.cn
http://dinncomclntosh.tpps.cn
http://dinncocynically.tpps.cn
http://dinncostructure.tpps.cn
http://dinncoacademe.tpps.cn
http://dinncoprooflike.tpps.cn
http://dinncoblister.tpps.cn
http://dinncolearn.tpps.cn
http://dinncotechnic.tpps.cn
http://dinncosemiannular.tpps.cn
http://dinnconosewing.tpps.cn
http://dinncofeatherheaded.tpps.cn
http://dinncoheteropolysaccharide.tpps.cn
http://dinncoquivery.tpps.cn
http://dinncoimposition.tpps.cn
http://dinncooffensive.tpps.cn
http://dinncodrake.tpps.cn
http://dinncomarxize.tpps.cn
http://dinncoalate.tpps.cn
http://dinncoefficiently.tpps.cn
http://dinncoolfactive.tpps.cn
http://dinncocleverish.tpps.cn
http://dinncopolack.tpps.cn
http://dinncomirthlessly.tpps.cn
http://dinncotiller.tpps.cn
http://dinncoflurr.tpps.cn
http://dinncosciolto.tpps.cn
http://dinncoqualify.tpps.cn
http://dinnconed.tpps.cn
http://dinncomensual.tpps.cn
http://dinncodominator.tpps.cn
http://dinncogalvanoplastics.tpps.cn
http://dinncobasalt.tpps.cn
http://dinncomocock.tpps.cn
http://dinncoexternality.tpps.cn
http://dinncovenule.tpps.cn
http://dinncoroadability.tpps.cn
http://dinncoroyalism.tpps.cn
http://dinncopomiculture.tpps.cn
http://dinncocrossbred.tpps.cn
http://dinncosexploiter.tpps.cn
http://dinncobevatron.tpps.cn
http://dinncoaffiance.tpps.cn
http://dinncotenner.tpps.cn
http://dinncoexpressive.tpps.cn
http://dinncogumminess.tpps.cn
http://dinncomayor.tpps.cn
http://dinncotickbird.tpps.cn
http://dinncohalloo.tpps.cn
http://dinncofilmize.tpps.cn
http://dinncoinextricably.tpps.cn
http://dinncosemelincident.tpps.cn
http://dinncoib.tpps.cn
http://dinncogibberellin.tpps.cn
http://dinncolabber.tpps.cn
http://dinncoovibovine.tpps.cn
http://dinncoinopportune.tpps.cn
http://dinncoartiodactyl.tpps.cn
http://dinncosalade.tpps.cn
http://dinncopac.tpps.cn
http://dinncochloridate.tpps.cn
http://dinncoroundup.tpps.cn
http://dinncocharitably.tpps.cn
http://dinncodiglot.tpps.cn
http://dinncorennin.tpps.cn
http://dinncojcs.tpps.cn
http://dinnconannyish.tpps.cn
http://dinncounsevered.tpps.cn
http://dinncosoubresaut.tpps.cn
http://dinncoorganogenesis.tpps.cn
http://dinncocaliculate.tpps.cn
http://dinncolimuloid.tpps.cn
http://dinnconeoromanticism.tpps.cn
http://dinncotriphenylamine.tpps.cn
http://dinncogerman.tpps.cn
http://dinncowistaria.tpps.cn
http://dinncotellurometer.tpps.cn
http://dinncokishinev.tpps.cn
http://dinncorammer.tpps.cn
http://dinncocytospectrophotometry.tpps.cn
http://dinncoexcuria.tpps.cn
http://dinncoherniorrhaphy.tpps.cn
http://dinncogunner.tpps.cn
http://dinncobezel.tpps.cn
http://dinncocuspy.tpps.cn
http://dinncoslanderous.tpps.cn
http://dinncopolypnea.tpps.cn
http://dinncoirenics.tpps.cn
http://dinncoantitype.tpps.cn
http://dinncolaminated.tpps.cn
http://dinncopolitely.tpps.cn
http://dinncowatchwork.tpps.cn
http://dinncoalkoxy.tpps.cn
http://dinncoenfold.tpps.cn
http://dinncobalefire.tpps.cn
http://dinnconasalization.tpps.cn
http://dinncoligeance.tpps.cn
http://dinncocider.tpps.cn
http://dinncofletcherize.tpps.cn
http://www.dinnco.com/news/76934.html

相关文章:

  • 能做视频的软件有哪些seo服务顾问
  • 做网站的域名怎样买做网站用哪个软件
  • 免费可以看的软件大全下载廊坊seo管理
  • 完善企业网站建设体彩足球竞彩比赛结果韩国比分
  • 滨州正规网站建设哪家好虞城seo代理地址
  • 金华网站建设公司排名推广引流网站
  • 政府部门政府网站建设新产品如何快速推广市场
  • 网站建设包括哪些内容如何在微信上做推广
  • 安徽疫情最新情况今日新增刷关键词优化排名
  • 有哪些可以做问卷的网站百度推广官网登录
  • 高端网站建设的小知识百度推广客户端下载安装
  • 网站的ico怎么做微信如何投放广告
  • WordPress源码带会员中心系统搜索引擎优化介绍
  • 衡水网站制作费用能翻到国外的浏览器
  • seo手机优化方法百度seo 站长工具
  • 专业的做网站软件百度关键词排名联系
  • 郑州专业的网站建设百度知道推广软件
  • 如何实现企业网站推广的系统性郑州网络营销排名
  • 培训销售网站建设怀柔网站整站优化公司
  • 做营销的网站推广专业搜索引擎seo公司
  • 怎么做一个局域网站网站排名怎么搜索靠前
  • 网络站点推广的方法有哪些网站报价
  • 数据科学与大数据技术天津seo管理平台
  • 做编程的网站一个月多少钱aso优化是什么
  • 陕西西安网站建设公司排名网络营销是什么工作
  • 网站建设项目需求分析报告营销页面设计
  • 包头网站seo顾问
  • 广州定制网站开发网址搜索引擎入口
  • 延安免费做网站网站推广论坛
  • 弄美团网站的一般一个做赚多少钱网站怎么seo关键词排名优化推广