SpringMVC?Restful風格與中文亂碼問題解決方案介紹
基本要點
1、定義
根據(jù)百度百科的定義,RESTFUL是一種網(wǎng)絡(luò)應用程序的設(shè)計風格和開發(fā)方式
2、傳統(tǒng)方式與Restful風格的區(qū)別
在我們學習restful風格之前,我們請求接口,都是使用http://localhost:8080/controller?method=add這種方式攜帶接口所需要的參數(shù)
而調(diào)用restful風格的接口時,我們可以改成http://localhost:8080/controller/add這種類型
3、如何使用Restful風格
我們通過一個代碼demo來了解一下它的使用方法
首先,我們設(shè)置當前module為web項目,在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/>
,會在Spring MVC上下文中定義一個org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler,它會像一個檢查員,對DispatcherServlet的請求進行處理,如果該請求已經(jīng)作了映射,那么會接著交給后臺對應的處理程序,如果沒有作映射,就交給 WEB 應用服務器默認的 Servlet 處理,從而找到對應的靜態(tài)資源,只有再找不到資源時才會報錯
<?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"> <!-- 開啟自動掃描,讓指定包下的注解生效,由IOC容器統(tǒng)一管理 --> <context:component-scan base-package="com.decade3.controller"/> <!-- 支持mvc注解驅(qū)動 --> <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>
接著我們在WEB-INF下新建一個jsp文件夾,在下面新建一個rest.jsp頁面,寫一個form表單,點擊按鈕觸發(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>
和一個rest2.jsp頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${msg} </body> </html>
最后我們寫一個控制器類HelloController.java,使用相同的路徑但是使用不同的請求方法
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"; } }
最后我們啟動tomcat驗證一下,如果出現(xiàn)報錯請參考我之前的博客SpringMVC執(zhí)行過程詳細講解
在地址欄直接輸入url是get請求,所以我們走的是test1方法,頁面會跳轉(zhuǎn)到rest.jsp
我們點擊提交按鈕,會觸發(fā)調(diào)用post方法,走test2,跳轉(zhuǎn)到rest2.jsp
如圖所示,結(jié)果符合我們的預期
- 關(guān)于@PathVariable注解
我們可以通過@PathVariable將url中的參數(shù)與方法上的參數(shù)綁定起來
- 關(guān)于請求方法類型
我們可以使用POST、DELETE、PUT、GET,使用不同方法對資源進行操作。
分別對應 添加、 刪除、修改、查詢
我們可以通過限制method類型,來實現(xiàn)url請求地址的復用
如上圖中,我們可以都使用http://localhost:8080/restful/add/a/b的形式,通過限制方法去調(diào)用不同的接口
- 控制器類中的@PostMapping(value = “/add/{a}/”)和@RequestMapping(value = “/add/{a}/”, method = RequestMethod.POST)是一樣
除了@PostMapping之外,常用的還有
@GetMapping
@PutMapping
@DeleteMapping
@PatchMapping
4、為什么要用restful
我個人認為,使用restful風格的接口,使得我們的請求路徑更加簡潔,而且相同的接口可以通過限制請求方式實現(xiàn)不同的功能,增加了代碼的復用性,最后,restful風格的參數(shù)是直接拼接在url上的,我們不需要對參數(shù)做出解釋,提升了安全性
5、亂碼問題
有時候我們使用post請求時,如果參數(shù)中攜帶中文漢字,可能會出現(xiàn)解析亂碼的情況
這個時候,我們就可以使用spring提供的過濾器來解決,我們需要在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風格與中文亂碼問題解決方案介紹的文章就介紹到這了,更多相關(guān)SpringMVC Restful內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java SSM框架(Spring+SpringMVC+MyBatis)搭建過程
最近一段時間搭建了ssm環(huán)境,并測試了幾個小項目,下面小編通過圖文并茂的形式給大家分享Java SSM框架(Spring+SpringMVC+MyBatis)搭建過程,需要的朋友參考下吧2017-11-11springboot中如何通過main方法調(diào)用service或dao
這篇文章主要介紹了springboot中如何通過main方法調(diào)用service或dao,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02kafka?消息隊列中點對點與發(fā)布訂閱的區(qū)別說明
這篇文章主要介紹了kafka?消息隊列中點對點與發(fā)布訂閱的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05Java動態(tài)數(shù)組添加數(shù)據(jù)的方法與應用示例
這篇文章主要介紹了Java動態(tài)數(shù)組添加數(shù)據(jù)的方法,結(jié)合實例形式詳細分析了Java動態(tài)數(shù)組的創(chuàng)建、添加、查找、打印等相關(guān)操作技巧,需要的朋友可以參考下2019-11-11解決運行jar包出錯:ClassNotFoundException問題
這篇文章主要介紹了解決運行jar包出錯:ClassNotFoundException問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12Java8新特性之Collectors.joining()實例詳解
在項目中我們常常要對list集合的數(shù)據(jù)做一些字符串拼接/處理等相關(guān)操作,下面這篇文章主要給大家介紹了關(guān)于Java8新特性之Collectors.joining()的相關(guān)資料,需要的朋友可以參考下2023-01-01