国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當(dāng)前位置: 首頁 > news >正文

找人做網(wǎng)站價格鄭州seo網(wǎng)絡(luò)營銷

找人做網(wǎng)站價格,鄭州seo網(wǎng)絡(luò)營銷,智能客服系統(tǒng),網(wǎng)站建設(shè)優(yōu)化服務(wù)策劃標(biāo)簽:Rest.攔截器.swagger.測試; 一、簡介 基于web包的依賴,SpringBoot可以快速啟動一個web容器,簡化項(xiàng)目的開發(fā); 在web開發(fā)中又涉及如下幾個功能點(diǎn): 攔截器:可以讓接口被訪問之前,將請求攔截…

標(biāo)簽:Rest.攔截器.swagger.測試;

一、簡介

基于web包的依賴,SpringBoot可以快速啟動一個web容器,簡化項(xiàng)目的開發(fā);

web開發(fā)中又涉及如下幾個功能點(diǎn):

攔截器:可以讓接口被訪問之前,將請求攔截到,通過對請求的識別和校驗(yàn),判斷請求是否允許通過;

頁面交互:對于服務(wù)端的開發(fā)來說,需要具備簡單的頁面開發(fā)能力,解決部分場景的需求;

Swagger接口:通過簡單的配置,快速生成接口的描述,并且提供對接口的測試能力;

Junit測試:通過編寫代碼的方式對接口進(jìn)行測試,從而完成對接口的檢查和驗(yàn)證,并且可以不入侵原代碼結(jié)構(gòu);

二、工程搭建

1、工程結(jié)構(gòu)

2、依賴管理

<!-- 基礎(chǔ)框架組件 -->
<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開發(fā)

1、接口開發(fā)

編寫四個簡單常規(guī)的接口,從對資源操作的角度,也就是常說的:增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、頁面交互

對于服務(wù)端開發(fā)來說,在部分場景下是需要進(jìn)行簡單的頁面開發(fā)的,比如通過頁面渲染再去生成文件,或者直接通過頁面填充郵件內(nèi)容等;

數(shù)據(jù)接口

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

頁面解析:分別解析了普通參數(shù),實(shí)體對象,集合容器,數(shù)組容器等幾種數(shù)據(jù)模型;

<div style="text-align: center"><hr/><h5>普通參數(shù)解析</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>數(shù)組容器解析</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、攔截器定義

通過實(shí)現(xiàn)HandlerInterceptor接口,完成對兩個攔截器的自定義,請求在訪問服務(wù)時,必須通過兩個攔截器的校驗(yàn);

/*** 攔截器一*/
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工程的配置文件中,可以通過實(shí)現(xiàn)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測試

在個人的習(xí)慣上,Swagger接口文檔更偏向在前后端對接的時候使用,而Junit單元測試更符合開發(fā)的時候使用,這里是對RestWeb中的接口進(jìn)行測試;

@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 {// 參數(shù)模型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 {// 參數(shù)模型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("響應(yīng)-status【"+mvcResult.getResponse().getStatus()+"】");System.out.println("響應(yīng)-content【"+mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8)+"】");}
}

六、參考源碼

文檔倉庫:
https://gitee.com/cicadasmile/butte-java-note源碼倉庫:
https://gitee.com/cicadasmile/butte-spring-parent
http://m.aloenet.com.cn/news/42226.html

相關(guān)文章:

  • 響應(yīng)式自適應(yīng)網(wǎng)站模板杭州seo培訓(xùn)
  • 嘉興網(wǎng)站建設(shè)下載app
  • 深圳人才市場招聘網(wǎng)最新招聘信息疫情二十條優(yōu)化措施
  • 個人網(wǎng)站做淘寶客教程網(wǎng)絡(luò)營銷課程培訓(xùn)課程
  • 鄂爾多斯網(wǎng)站建設(shè)高端大氣網(wǎng)站建設(shè)
  • 濟(jì)南建設(shè)委員會網(wǎng)站網(wǎng)站收錄一鍵提交
  • 2022中國互聯(lián)網(wǎng)公司排名網(wǎng)站seo置頂 樂云踐新專家
  • 網(wǎng)站開發(fā)圖片建站公司網(wǎng)站源碼
  • 裝修推薦平臺星沙網(wǎng)站優(yōu)化seo
  • 百度不收錄手機(jī)網(wǎng)站嗎微信小程序開發(fā)一個多少錢啊
  • 沒有網(wǎng)站可以域名備案嗎西安網(wǎng)站seo診斷
  • 中國做的最好的網(wǎng)站有哪些百度競價托管公司
  • 西安那里做網(wǎng)站wordpress外貿(mào)獨(dú)立站
  • 百度網(wǎng)站上傳平面設(shè)計網(wǎng)站
  • web做網(wǎng)站騰訊域名注冊官網(wǎng)
  • 網(wǎng)站建設(shè)的自查整改報告沈陽seo團(tuán)隊(duì)
  • 重慶網(wǎng)站建設(shè)價格網(wǎng)壇最新排名
  • 網(wǎng)站推廣一般怎么做短視頻運(yùn)營是做什么的
  • 天津的網(wǎng)站建設(shè)公司鎮(zhèn)江網(wǎng)站制作公司
  • 博彩外圍網(wǎng)站怎么做網(wǎng)站服務(wù)器多少錢一年
  • 做網(wǎng)站只有搜網(wǎng)址嗎搜索量排名
  • 帝國cms如何做微網(wǎng)站免費(fèi)seo網(wǎng)站診斷免費(fèi)
  • wordpress 捐贈葉濤網(wǎng)站推廣優(yōu)化
  • 自己dreamweaver做的網(wǎng)站怎么掛seo內(nèi)部優(yōu)化方案
  • 工業(yè)和信息化部人才交流中心長沙百度快速排名優(yōu)化
  • 應(yīng)聘網(wǎng)站建設(shè)工程師semifinal
  • 軟件商城app下載安裝seo的優(yōu)化策略有哪些
  • 廣州 網(wǎng)站制搜索引擎營銷流程是什么?
  • 網(wǎng)站建設(shè)需要服務(wù)器支持 嗎seo發(fā)貼軟件
  • 茂名網(wǎng)站建設(shè)技術(shù)托管搜索引擎優(yōu)化的意思