SpringMVC中REST介绍以及如何实现REST请求

 时间:2026-02-15 00:42:59

1、Springmvc提供了一个filter,可以把delete请求和put请求转化成post请求。

在web.xml中配置过滤器:org.springframework.web.filter.HiddenHttpMethodFilter:

<!-- 可以把POST请求转为为DELETE或者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中REST介绍以及如何实现REST请求

2、发送一个get请求:

<h2>

      <a href="springmvc/helloworld/1">Test Rest GET</a>

</h2>

SpringMVC中REST介绍以及如何实现REST请求

3、发送一个post请求:

<form action="springmvc/helloworld/1" method="post">

     <input type="submit" value="Test REST POST"/>

</form>

SpringMVC中REST介绍以及如何实现REST请求

4、发送一个put请求,发送put请求的时候,我们需求添加一个隐藏域。

<form action="springmvc/helloworld/1" method="post">

      <input type="hidden" name="_method" value="PUT" />

      <input type="submit" value="Test REST PUT"/>

</form>

SpringMVC中REST介绍以及如何实现REST请求

5、发送一个DELETE请求,发送DELETE请求的时候,我们需求添加一个隐藏域。

  <input type="hidden" name="_method" value="DELETE" />

<form action="springmvc/helloworld/1" method="post">

      <input type="hidden" name="_method" value="DELETE" />

      <input type="submit" value="Test REST DELETE"/>

</form>

SpringMVC中REST介绍以及如何实现REST请求

6、springmvc控制层的代码如下如下:

package com.gwolf.springmvc.handlers;

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;

@Controller

@RequestMapping("/springmvc")

public class HelloWorldController {

                

        @RequestMapping(value="/helloworld/{id}",method=RequestMethod.GET)

        public String hello(@PathVariable Integer id) {

                System.out.println("test rest get:" + id);

                

                return "success";

        }

        

        @RequestMapping(value="/helloworld",method=RequestMethod.POST)

        public String hello() {

                System.out.println("test POST:" );

                

                return "success";

        }

        

        @RequestMapping(value="/helloworld/{id}",method=RequestMethod.DELETE)

        public String helloDelete(@PathVariable Integer id) {

                System.out.println("test rest delete:" + id);

                

                return "success";

        }

        

        @RequestMapping(value="/helloworld/{id}",method=RequestMethod.PUT)

        public String helloPUt(@PathVariable Integer id) {

                System.out.println("test rest put:" + id);

                

                return "success";

        }

}

SpringMVC中REST介绍以及如何实现REST请求

7、运行tomcat,查看各个请求是否正确相应:

SpringMVC中REST介绍以及如何实现REST请求

  • 如何在 Java 中实现 REST 服务?
  • 如何使用Postman进行rest的请求测试
  • RESTclient使用
  • firefox使用RestClient发送get请求
  • spring注解之RestController
  • 热门搜索
    宫保鸡丁的做法家常菜 大头菜的做法 胖头鱼头的做法大全 饺子馅做法大全 牛蛙的做法 绿豆芽的做法 吊兰怎么养才能更旺盛 羌怎么读 西兰花的做法 板栗烧鸡的做法