SpringMVC学习(三)RestFul风格

本文详细介绍了RestFul风格API的设计理念,包括资源定位、操作方法(GET、POST、PUT、DELETE)的使用,以及如何在SpringMVC框架中实现RestFul风格的Controller。通过具体实例展示了如何配置环境、创建控制器并进行测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RestFul风格
1、什么是RestFul风格?
Restful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
2、RestFul功能
资源:互联网中所有的东西都可以被抽象定义为资源。
操作资源 :使用 POST、DELETE、PUT、GET方法对资源进行不同的操作。

使用传统的方式对资源进行操作: 不同的参数对应的操作不同。

http://127.0.0.1/item/queryItem.action?id=1 查询,GET
http://127.0.0.1/item/saveItem.action 新增,POST
http://127.0.0.1/item/updateItem.action 更新,POST
http://127.0.0.1/item/deleteItem.action?id=1 删除,GET或POST

使用RestFul风格方式对资源进行操作: 同一个请求地址可能对应的功能就不同。

http://127.0.0.1/item/1 查询,GET
http://127.0.0.1/item 新增,POST
http://127.0.0.1/item 更新,PUT
http://127.0.0.1/item/1 删除,DELETE
3、测试

搭建环境

  1. web.xml中注册 DisPatchServlet
<?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>SpringMVC</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>SpringMVC</servlet-name>
        <!--拦截所有的请求-->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
  1. 创建对应的Spring配置文件springmvc-servlet.xml,配置自动注入HandlerMapping、HandlerAdapter,注入视图解析器。
<?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">

    <context:component-scan base-package="com.zyh.controller"/>
    <mvc:default-servlet-handler/>
    <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>

    <bean name="/test" class="com.zyh.controller.HelloController"/>
</beans>
  1. 编写要请求的JSP页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${msg}
</body>
</html>
  1. 编写RestFulController类
package com.zyh.controller;

import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
    //映射的访问路径
    @RequestMapping("/test2")
    public String test(@Nullable Model model) {
        //给视图中传递数据 
        model.addAttribute("msg","TestController2");
        return "test";
    }
}

开启服务器测试:

这是使用传统的方式进行访问,参数传递如下图
在这里插入图片描述
使用RestFul风格进行传参

  • 要在Controller中定义访问的路径映射:@PathVariable 注解,让方法参数的值对应绑定到一个URI模板变量上。
@Controller
public class RestFulController {
    @RequestMapping("/hello/{a}/{b}")
    public String test1(@PathVariable int a, @PathVariable String b, Model model) {
        String result = a + b;
        model.addAttribute("msg","test1结果="+result);
        return "test";
    }
}
  • 测试
    在这里插入图片描述

  • 使用method属性指定请求类型

用于约束请求的类型,可以收窄请求范围。指定请求谓词的类型如GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE等

基于方法的组合注解: 就相当于@RequestMapping( method = RequestMethod.GET)

@GetMapping   
@PostMapping 
@DeleteMapping 
@PutMapping

增加一个方法:

    @GetMapping("/hello/{a}/{b}")
    public String test1(@PathVariable int a,@PathVariable String b, Model model) {
        String result = a + b;
        model.addAttribute("msg","test1结果="+result);
        return "test";
    }

测试:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值