• Java框架(七)-- RESTful风格的应用(1)--概述及开发RESTful Web应用


    RESTful开发风格

    传统Web应用

    在这里插入图片描述

    REST与RESTful

    REST - 表现层状态转换,资源在网络中以某种表现形式进行状态转换。
    RESTful是基于REST理念的一套开发风格,是具体的开发规则。

    RESTful传输数据

    在这里插入图片描述

    RESTful开发规范

    使用URL作为用户交互入口。
    明确的语义规范(GET | POST | PUT | DELETE)。
    只返回数据(JSON | XML),不包含任何展现。

    RESTful命名要求

    在这里插入图片描述

    开发RESTful Web应用

    打开IDEA,新建maven web工程,引入SpringMVC依赖
    在这里插入图片描述
    在web.xml配置SpringMVC

    
    <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_3_1.xsd"
             version="3.1">
        <servlet>
            <servlet-name>springmvcservlet-name>
            
            <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
            <init-param>
                <param-name>contextConfigLocationparam-name>
                <param-value>classpath:applicationContext.xmlparam-value>
            init-param>
            
            <load-on-startup>0load-on-startup>
        servlet>
        <servlet-mapping>
            <servlet-name>springmvcservlet-name>
            
            <url-pattern>/url-pattern>
        servlet-mapping>
        <filter>
            <filter-name>characterFilterfilter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
            <init-param>
                <param-name>encodingparam-name>
                <param-value>UTF-8param-value>
            init-param>
        filter>
        <filter-mapping>
            <filter-name>characterFilterfilter-name>
            <url-pattern>/*url-pattern>
        filter-mapping>
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    在resources目录下创建applicationContext.xml

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           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
            https://www.springframework.org/schema/context/spring-context.xsd">
        <context:component-scan base-package="com.ql.restful"/>
        
        <mvc:annotation-driven>
            <mvc:message-converters>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            
                            <value>text/html;charset=utf-8value>
                            <value>application/json;charset=utf-8value>
                        list>
                    property>
                bean>
            mvc:message-converters>
        mvc:annotation-driven>
        
        <mvc:default-servlet-handler/>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    在com.ql.restful.controller包下创建RestfulController.java类

    package com.ql.restful.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    @RequestMapping("/restful")
    public class RestfulController {
        @GetMapping("request")
        @ResponseBody
        public String doGetRequest(){
            return "{\"message\":\"返回查询结果\"}";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    启动项目,在浏览器地址栏中输入http://localhost:8080/restful/request
    在这里插入图片描述

    实现RESTful实验室

    在webapp目录下引入jquery-3.6.0.min.js,并在该目录创建client.html

    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>RESTful实验室title>
        <script src="jquery-3.6.0.min.js">script>
        <script>
            $(function (){
                $("#btnGet").click(function (){
                    $.ajax({
                        url:"/restful/request",
                        type:"get",
                        dataType:"json",
                        success:function (json){
                            $("#message").text(json.message);
                        }
                    })
                })
                $("#btnPost").click(function (){
                    $.ajax({
                        url:"/restful/request",
                        type:"post",
                        dataType:"json",
                        success:function (json){
                            $("#message").text(json.message);
                        }
                    })
                })
                $("#btnPut").click(function (){
                    $.ajax({
                        url:"/restful/request",
                        type:"put",
                        dataType:"json",
                        success:function (json){
                            $("#message").text(json.message);
                        }
                    })
                })
                $("#btnDelete").click(function (){
                    $.ajax({
                        url:"/restful/request",
                        type:"delete",
                        dataType:"json",
                        success:function (json){
                            $("#message").text(json.message);
                        }
                    })
                })
            })
        script>
    head>
    <body>
        <input type="button" id="btnGet" value="发送Get请求">
        <input type="button" id="btnPost" value="发送Post请求">
        <input type="button" id="btnPut" value="发送Put请求">
        <input type="button" id="btnDelete" value="发送Delete请求">
        <h1 id="message">h1>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    修改RestfulController

    package com.ql.restful.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.*;
    
    @Controller
    @RequestMapping("/restful")
    public class RestfulController {
        @GetMapping("request")
        @ResponseBody
        public String doGetRequest(){
            return "{\"message\":\"返回查询结果\"}";
        }
    
        @PostMapping("request")
        @ResponseBody
        public String doPostRequest(){
            return "{\"message\":\"数据新建成功\"}";
        }
    
        @PutMapping("request")
        @ResponseBody
        public String doPutRequest(){
            return "{\"message\":\"数据更新成功\"}";
        }
    
        @DeleteMapping("request")
        @ResponseBody
        public String doDeleteRequest(){
            return "{\"message\":\"数据删除成功\"}";
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    运行项目测试
    在这里插入图片描述

    RestController注解与路径变量

    @RestController在Controller类上添加,替换@Controller注解,让每个方法放回json数据,方法上无需再用@ResponseBody注解。
    修改RestfulController,使用RestController注解与路径变量

    package com.ql.restful.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.*;
    
    @RestController
    @RequestMapping("/restful")
    public class RestfulController {
        @GetMapping("request")
    //    @ResponseBody
        public String doGetRequest(){
            return "{\"message\":\"返回查询结果\"}";
        }
    
        @PostMapping("request/{rid}")
    //    @ResponseBody
        public String doPostRequest(@PathVariable("rid") Integer requestId){
            return "{\"message\":\"数据新建成功\",\"id\":"+requestId+"}";
        }
    
        @PutMapping("request")
    //    @ResponseBody
        public String doPutRequest(){
            return "{\"message\":\"数据更新成功\"}";
        }
    
        @DeleteMapping("request")
    //    @ResponseBody
        public String doDeleteRequest(){
            return "{\"message\":\"数据删除成功\"}";
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    再修改client.html中post请求部分

                $("#btnPost").click(function (){
                    $.ajax({
                        url:"/restful/request/100",
                        type:"post",
                        dataType:"json",
                        success:function (json){
                            $("#message").text(json.message+":"+json.id);
                        }
                    })
                })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    运行测试
    在这里插入图片描述

  • 相关阅读:
    谈谈我对服务网格的理解
    STL1(C++标准模板库)
    亚马逊按关键字搜索商品 API 返回值说明
    Node.js 是如何处理请求的
    matplotlib制图进阶版
    初学Vue3(个人学习)
    2023年面试测试工程师一般问什么问题?
    蛇形填空 I
    Java之图书管理系统
    iNeuOS工业互联网操作系统,数据点、设备和业务的计算与预警
  • 原文地址:https://blog.csdn.net/qq_32091929/article/details/126077181