前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >#yyds干货盘点#SpringMVC rest风格编程案例

#yyds干货盘点#SpringMVC rest风格编程案例

作者头像
用户9184480
发布2024-12-19 09:44:38
发布2024-12-19 09:44:38
8700
代码可运行
举报
文章被收录于专栏:云计算linux云计算linux
运行总次数:0
代码可运行

一,什么是RESTful

RESTful(RESTful Web Services)一种架构风格,表述性状态转移,它不是一个软件,也不是一个标准,而是一种思想,不依赖于任何通信协议,但是开发时要成功映射到某协议时也需要遵循其标准,但不包含对通信协议的更改

 特征:

    1.通过url地址来标识资源,系统中的每个对象或资源都可以通过其url地址来获取

    2.统一接口,显式地使用HTTP方法,来进行crud(create,update,insert,delete)映射

      创建资源使用POST

      更新资源使用PUT

      检索资源使用GET

      删除资源使用DELETE

    3.资源多重反映.通过url地址访问的每个资源都可以根据客户端的规定进行返回,例:JSON,XML

RESTful服务适用web应用中创建服务的API,将资源以JSON或XML等数据格式进行暴露,从而可以更方便的让客户端进行调用

二.基于SpringMVC的RESTful服务

  在SpringMVC中对RESTful支持,主要通过注解来实现

  @Controller:声明一个处理请求的控制器

  @RequestMapping:请求映射地址到对应的方法,该注解又可以分为一下几种类型:

    @GetMapping

    @PostMpping

    @PutMapping

    @DeleteMapping

    @PatchMapping

  @ResponsrBody:响应内容转换为JSON格式

  @RequestBody:请求内容转换为JSON格式

  @RestContrller:等同@Controller+@ResponsrBody

前台跳转代码参考如下所示:

代码语言:javascript
代码运行次数:0
复制
<%--
  Created by IntelliJ IDEA.
  User: qyq
  Date: 2022/2/21
  Time: 19:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

    <a href="user/findUser/lisi">测试</a>
    <form action="emp" method="post">
        账户:<input name="name" id="name" ><br/>
        编号:<input name="id" id="id"><br/>
        <button type="submit">登录</button>
    </form>

    <form action="emp/1" method="post">
        <input name="_method" value="delete">
        <input type="submit" value="删除一号图书" >
    </form>
    <form action="emp/1" method="post">
        <input name="_method" value="put">
        <input type="submit" value="更新一号图书"  >
    </form>
</body>
</html>

Emp实体类参考如下所示:

代码语言:javascript
代码运行次数:0
复制
package com.aaa.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.stereotype.Component;
@Data
@AllArgsConstructor
@Component
public class Emp {
    private Integer id;
    private String name;
}

控制器EmpController参考如下:

代码语言:javascript
代码运行次数:0
复制
package com.aaa.controller;

import com.aaa.entity.Emp;
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.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
public class EmpController {
    @RequestMapping(value = "/emp",method = RequestMethod.GET)
    public ModelAndView getAllEmps(){
        ModelAndView mv=new ModelAndView();
        List<Emp> empList=new ArrayList<Emp>();
        empList.add(new Emp(1,"张三"));
        empList.add(new Emp(2,"李四"));
        mv.addObject("emps",empList);
        mv.setViewName("emplist");
        return mv;
    }
    @RequestMapping(value = "/emp/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public String deleteEmp(@PathVariable("id")Integer id){
        System.out.println("调用service层业务...");
        return "emplist";
    }
    @RequestMapping(value = "/emp",method = RequestMethod.POST)

    public String addEmp(Emp emp){
        System.out.println("增加数据...");
        return "emplist";
    }
    @RequestMapping(value = "/emp/{id}",method = RequestMethod.PUT)
    @ResponseBody
    public String editEmp(@PathVariable Integer id) {
        System.out.println("编辑数据");
        return "emplist";
    }
}

针对PUT和DELETE操作可能会出问题,需要在web.xml中进行配置

代码语言:javascript
代码运行次数:0
复制
<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>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-02-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档