SpringMVC?Restful風(fēng)格與中文亂碼問(wèn)題解決方案介紹
基本要點(diǎn)
1、定義
根據(jù)百度百科的定義,RESTFUL是一種網(wǎng)絡(luò)應(yīng)用程序的設(shè)計(jì)風(fēng)格和開(kāi)發(fā)方式
2、傳統(tǒng)方式與Restful風(fēng)格的區(qū)別
在我們學(xué)習(xí)restful風(fēng)格之前,我們請(qǐng)求接口,都是使用http://localhost:8080/controller?method=add這種方式攜帶接口所需要的參數(shù)
而調(diào)用restful風(fēng)格的接口時(shí),我們可以改成http://localhost:8080/controller/add這種類型
3、如何使用Restful風(fēng)格
我們通過(guò)一個(gè)代碼demo來(lái)了解一下它的使用方法
首先,我們?cè)O(shè)置當(dāng)前module為web項(xiàng)目,在web.xml中配置一下DispatcherServlet
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
然后我們配置一下springmvc的配置文件springmvc-servlet.xml
這里使用<mvc:annotation-driven/>
,會(huì)在Spring MVC上下文中定義一個(gè)org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler,它會(huì)像一個(gè)檢查員,對(duì)DispatcherServlet的請(qǐng)求進(jìn)行處理,如果該請(qǐng)求已經(jīng)作了映射,那么會(huì)接著交給后臺(tái)對(duì)應(yīng)的處理程序,如果沒(méi)有作映射,就交給 WEB 應(yīng)用服務(wù)器默認(rèn)的 Servlet 處理,從而找到對(duì)應(yīng)的靜態(tài)資源,只有再找不到資源時(shí)才會(huì)報(bào)錯(cuò)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 開(kāi)啟自動(dòng)掃描,讓指定包下的注解生效,由IOC容器統(tǒng)一管理 --> <context:component-scan base-package="com.decade3.controller"/> <!-- 支持mvc注解驅(qū)動(dòng) --> <mvc:annotation-driven/> <!-- 讓Spring MVC不處理靜態(tài)資源 --> <mvc:default-servlet-handler/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
接著我們?cè)赪EB-INF下新建一個(gè)jsp文件夾,在下面新建一個(gè)rest.jsp頁(yè)面,寫(xiě)一個(gè)form表單,點(diǎn)擊按鈕觸發(fā)post方法
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${msg} <form action="/restful/add/spring-/mvc" method="post"> <input type="submit"> </form> </body> </html>
和一個(gè)rest2.jsp頁(yè)面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${msg} </body> </html>
最后我們寫(xiě)一個(gè)控制器類HelloController.java,使用相同的路徑但是使用不同的請(qǐng)求方法
package com.decade3.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping(value = "/restful") public class HelloController { @RequestMapping(value = "/add/{a}/", method = RequestMethod.GET) public String test1(@PathVariable(value = "a") int a, @PathVariable(value = "b")int b, Model model) { int result = a + b; model.addAttribute("msg", result); return "rest"; } @PostMapping(value = "/add/{a}/") public String test2(@PathVariable(value = "a") String a, @PathVariable(value = "b")String b, Model model) { String result = a + b; model.addAttribute("msg", result); return "rest2"; } }
最后我們啟動(dòng)tomcat驗(yàn)證一下,如果出現(xiàn)報(bào)錯(cuò)請(qǐng)參考我之前的博客SpringMVC執(zhí)行過(guò)程詳細(xì)講解
在地址欄直接輸入url是get請(qǐng)求,所以我們走的是test1方法,頁(yè)面會(huì)跳轉(zhuǎn)到rest.jsp
我們點(diǎn)擊提交按鈕,會(huì)觸發(fā)調(diào)用post方法,走test2,跳轉(zhuǎn)到rest2.jsp
如圖所示,結(jié)果符合我們的預(yù)期
- 關(guān)于@PathVariable注解
我們可以通過(guò)@PathVariable將url中的參數(shù)與方法上的參數(shù)綁定起來(lái)
- 關(guān)于請(qǐng)求方法類型
我們可以使用POST、DELETE、PUT、GET,使用不同方法對(duì)資源進(jìn)行操作。
分別對(duì)應(yīng) 添加、 刪除、修改、查詢
我們可以通過(guò)限制method類型,來(lái)實(shí)現(xiàn)url請(qǐng)求地址的復(fù)用
如上圖中,我們可以都使用http://localhost:8080/restful/add/a/b的形式,通過(guò)限制方法去調(diào)用不同的接口
- 控制器類中的@PostMapping(value = “/add/{a}/”)和@RequestMapping(value = “/add/{a}/”, method = RequestMethod.POST)是一樣
除了@PostMapping之外,常用的還有
@GetMapping
@PutMapping
@DeleteMapping
@PatchMapping
4、為什么要用restful
我個(gè)人認(rèn)為,使用restful風(fēng)格的接口,使得我們的請(qǐng)求路徑更加簡(jiǎn)潔,而且相同的接口可以通過(guò)限制請(qǐng)求方式實(shí)現(xiàn)不同的功能,增加了代碼的復(fù)用性,最后,restful風(fēng)格的參數(shù)是直接拼接在url上的,我們不需要對(duì)參數(shù)做出解釋,提升了安全性
5、亂碼問(wèn)題
有時(shí)候我們使用post請(qǐng)求時(shí),如果參數(shù)中攜帶中文漢字,可能會(huì)出現(xiàn)解析亂碼的情況
這個(gè)時(shí)候,我們就可以使用spring提供的過(guò)濾器來(lái)解決,我們需要在web.xml中增加如下配置
<filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
到此這篇關(guān)于SpringMVC Restful風(fēng)格與中文亂碼問(wèn)題解決方案介紹的文章就介紹到這了,更多相關(guān)SpringMVC Restful內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringMVC使用RESTful接口案例詳解
- SpringMVC通過(guò)RESTful結(jié)構(gòu)實(shí)現(xiàn)頁(yè)面數(shù)據(jù)交互
- SpringMVC?RESTFul及REST架構(gòu)風(fēng)格介紹
- SpringMVC?RESTFul實(shí)體類創(chuàng)建及環(huán)境搭建
- SpringMVC?RESTFul實(shí)戰(zhàn)案例訪問(wèn)首頁(yè)
- SpringMVC?RESTFul實(shí)現(xiàn)列表功能
- SpringMVC?RESTFul實(shí)戰(zhàn)案例刪除功能實(shí)現(xiàn)
- SpringMVC使用RESTful接口案例
相關(guān)文章
Java SSM框架(Spring+SpringMVC+MyBatis)搭建過(guò)程
最近一段時(shí)間搭建了ssm環(huán)境,并測(cè)試了幾個(gè)小項(xiàng)目,下面小編通過(guò)圖文并茂的形式給大家分享Java SSM框架(Spring+SpringMVC+MyBatis)搭建過(guò)程,需要的朋友參考下吧2017-11-11mybatisplus中的xml對(duì)象參數(shù)傳遞問(wèn)題
這篇文章主要介紹了mybatisplus中的xml對(duì)象參數(shù)傳遞問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11springboot中如何通過(guò)main方法調(diào)用service或dao
這篇文章主要介紹了springboot中如何通過(guò)main方法調(diào)用service或dao,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02教你怎么用java一鍵自動(dòng)生成數(shù)據(jù)庫(kù)文檔
最近小編也在找這樣的插件,就是不想寫(xiě)文檔了,浪費(fèi)時(shí)間和心情啊,果然我找到一款比較好用,操作簡(jiǎn)單不復(fù)雜.screw 是一個(gè)簡(jiǎn)潔好用的數(shù)據(jù)庫(kù)表結(jié)構(gòu)文檔的生成工具,支持 MySQL、Oracle、PostgreSQL 等主流的關(guān)系數(shù)據(jù)庫(kù).需要的朋友可以參考下2021-05-05kafka?消息隊(duì)列中點(diǎn)對(duì)點(diǎn)與發(fā)布訂閱的區(qū)別說(shuō)明
這篇文章主要介紹了kafka?消息隊(duì)列中點(diǎn)對(duì)點(diǎn)與發(fā)布訂閱的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05Java動(dòng)態(tài)數(shù)組添加數(shù)據(jù)的方法與應(yīng)用示例
這篇文章主要介紹了Java動(dòng)態(tài)數(shù)組添加數(shù)據(jù)的方法,結(jié)合實(shí)例形式詳細(xì)分析了Java動(dòng)態(tài)數(shù)組的創(chuàng)建、添加、查找、打印等相關(guān)操作技巧,需要的朋友可以參考下2019-11-11解決運(yùn)行jar包出錯(cuò):ClassNotFoundException問(wèn)題
這篇文章主要介紹了解決運(yùn)行jar包出錯(cuò):ClassNotFoundException問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Java8新特性之Collectors.joining()實(shí)例詳解
在項(xiàng)目中我們常常要對(duì)list集合的數(shù)據(jù)做一些字符串拼接/處理等相關(guān)操作,下面這篇文章主要給大家介紹了關(guān)于Java8新特性之Collectors.joining()的相關(guān)資料,需要的朋友可以參考下2023-01-01