restful风格,简单概述,
1 请求路径相同,但请求方式不同。
eg:
path=“/user” post
save
path=”/user” put
update
2 请求路径和请求方式也相同,但参数不同。
eg:
path=”/user” get
findAll
path=“/user/{id}” get
findById(id)
前端请求链接:
localhost:8080/user/10 get
保存方法用post请求,查询方法用get请求方式,删除用delete请求方式,更新方法用put方式,但是 浏览器只支持get 和post请求,所以spring3.0版本提供了一个过滤器HiddenHttpMethodFilter ,用来做适配浏览器不支持的请求方式到后台,从而调用不同的方法。
如此,下面是restful代码的测试案例,简单易懂,耐心看下去,一篇文章带你体验并实战restful 风格编程。
1 配置文件
web.xml里
<!--控制中心 -->
<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.xml</param-value>
</init-param>
<!-- 第一次发请求servlet才会创建对象,现在是服务器启动的时候就会创建对象-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--使用Rest风格的URI,spring3.0提供的过滤器,将put,delete请求方式转换一下,因为前端浏览器只支持post和get 请求 ,这种方式比较麻烦,要通过form表单,写一个input hidden类型,name="_method" value="put" 这种,然后form 的method="post"
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>-->
<!--配置SpringMVC,把PUT或者DELETE请求转换成POST-->
<filter>
<filter-name>HttpPutFormContentFilter</filter-name>
<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HttpPutFormContentFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--包扫描 -->
<context:component-scan base-package="com.msyd"/>
<!-- 视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="suffix" value=".jsp"/>
<property name="prefix" value="/WEB-INF/admin/"/>
</bean>
<!-- 自定义类型转化器-->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.msyd.com.msyd.domain.String2Date4Convert"/>
</set>
</property>
</bean>
<!-- 打开springmvc支持的注解-->
<mvc:annotation-driven conversion-service="conversionService" />
</beans>
2 controller方法
package com.msyd;
import com.msyd.com.msyd.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("/restful")
public class RestfulController {
@RequestMapping(value = "/testPathVariable/{ids}",method = {RequestMethod.GET})
public String findById(@PathVariable("ids") String id ){
System.out.printf("restful-风格:findById,id%s",id);
return "success";
}
@RequestMapping(value="/testPathVariable",method = {RequestMethod.GET})
public String findAll(){
System.out.printf("restful-风格:findAll");
return "success";
}
@RequestMapping(value="/testPathVariable",method = {RequestMethod.POST})
public String save(@PathVariable("userName") String name,@PathVariable("age") int ages){
System.out.printf("restful-风格:save:name=%s,age=%s",name,ages);
return "success";
}
@RequestMapping(value="/testPathVariable")
public String update( ){
System.out.printf("restful-风格:update");
return "success";
}
}
3 页面
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/5/2
Time: 14:19
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>d</title>
</head>
<body>
save post , findAll get findById get, update put
<%--<form action="restful/testPathVariable" method="post">
登录名: <input type="text" name="username" value="海盗仙子"><br/>
密码:<input type="text" name="age" value="15"><br/>
<%–生成日期:<input type="text" name="creteTime" value="2019-11-11"><br/><br/>–%>
<input type="submit" value="save">
</form>--%>
<br/>
<a href="restful/testPathVariable/10001" methods="get">findByid</a> <br/>
<a href="restful/testPathVariable" methods="get">findAll</a><br/>
<a href="restful/testPathVariable" methods="put">update 用的put方式请求的update方法,但是好像不太好用。</a><br/>
<h3>浏览器只支持post和get请求,spring3提供 一个过滤器,可以指定请求方式,然后他负责自动转换为这个方式</h3><br/>
<%--<form action="restful/testPathVariable" method="post">
登录名: <input type="text" name="username" value="海盗仙子"><br/>
<input type="hidden" name="_method" value="PUT"><br/>
<input type="submit" value="update-HiddenHttpMethodFilter">
</form>--%>
<form action="restful/testPathVariable" method="put">
<input type="submit" value="update-HttpPutFormContentFilter">
</form>
</body>
</html>
测试结果就是controller里打印的,我是那个的idea,测put的请求方式,被搞惨,试过好几次,都打印的是findAll get请求方式,或者报错bad request …,后来终于是我预期的结果。可能是缓存的问题,没改代码,隔天就好了;