RestFul风格

RestFul的优势:简洁,高效,安全 还能实现url的复用

restful风格的注解

变量上的注解:@PathVariable

方法上的注解

@PostMapping("/h1/{a}/{b}") <==========> @RequestMapping(path = "/h1/{a}/{b}",method = RequestMethod.POST)
@GetMapping()
@PutMapping()
@DeleteMapping()
@PatchMapping()

首先要知道最初的传递参数的方式?a=1&b=2 ,这样的传参,暴露了我们的参数是不安全的,而restful风格的传参是/1/2,只是传递了值。

再写一遍web.xml文件和springmvc-servlet.xml文件,其实在我们的开发中这两个配置文件基本上是不需要在做改变的

web.xml文件

<?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">
    
    <!--注册DispatcherServlet 本质就是一个servlet-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <!--加载springmvc-servlet.xml文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>

        <!--配置web.xml文件的启动级别-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

springmvc-servlet.xml文件

<?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
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--扫描特定包下的注解-->
    <context:component-scan base-package="com.zkw.restful"/>
    <!--过滤静态资源-->
    <mvc:default-servlet-handler/>
    <!--mvc注解驱动-->
    <mvc:annotation-driven/>

    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${result}
</body>
</html>

RestfulController.java

package com.zkw.restful;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/restful")
public class RestfulController {

    @RequestMapping(path = "/h1/{a}/{b}",method = RequestMethod.POST)
    public String Restful(@PathVariable int a, @PathVariable String b, Model model){
        model.addAttribute("result","结果为"+(a+b));
        return "hello";
    }

   @GetMapping("/h1/{a}/{b}")
    public String Restful(@PathVariable int a, @PathVariable int b, Model model){
        model.addAttribute("result","结果为"+(a+b));
        return "hello";
    }

    //原始的风格
    @RequestMapping("/h2")
    public String Restful(String a, int b, Model model){
        model.addAttribute("result","结果为"+(a+b));
        return "hello";
    }

}

解释一下,在类上边的@RequestMapping("/restful")与这个类下边方法上的所有@RequestMapping("/h2")和 @GetMapping("/h1/{a}/{b}")是父子关系,相当于你要想访问这各类里边的方法,首先要先访问这个类。

原始风格的结果为

在这里插入图片描述

先说一下,它默认的提交方式是get,当我使用restful风格访问的时候,它走得是 @GetMapping("/h1/{a}/{b}")这个

在这里插入图片描述

最后再说一下url的复用,当我在另一个页面上使用post提交表单的时候它请求的方法是第一个

首先写一个测试页面test.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="/springmvc_04_restful_war_exploded/restful/h1/1/2" method="post">
    <input type="submit">
</form>
</body>
</html>

结果为

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值