zoukankan      html  css  js  c++  java
  • spring 基础(五) spring mvc RESTful

    1.RESTful

    1. 使用URL作为用户交互入口。
    2. 明确的语义规范(GET|POST|PUT|DELETE)。
    3. 只返回数据(JSON|XML),不包含任何展现。
    4. RESTful是一种设计理念:


    5. 命名规范:如图


    6. 5

    2.创建一个RESTful风格的SpringMvc

    1.创建一个maven项目。

    2.单击选中工程

    3.file=》Project Structure... ;注意此处路径修改必须统一。否则程序报错,比如都将web修改为srcmainwebapp

     4.因为当前是maven工程,不具有webapp开发能力。需要再Facets中添加web。记得单击web之后弹出页面选中工程,然后再确定

     

     5.确定web.xml存储路径一般放在是srcmainwebappWEB-INF目录下

     6.设置存储页面的路径

     7.添加artifact

    8.打开 Add Configuration..;然后先设置Templates 模板;先apply 然后ok。

     

     

    9.Templates 设置完毕之后,再打开 然后添加服务并且对齐设置

     

     

     

     

    10.启动,先再webapp下添加一个index.html

    11.pom导入springmvc相关

        <repositories>
            <repository>
                <id>aliyun</id>
                <name>aliyun</name>
                <url>https://maven.aliyun.com/repository/public</url>
            </repository>
        </repositories>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.2.6.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>5.2.6.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.16</version>
            </dependency>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>5.2.6.RELEASE</version>
            </dependency>
    
            <!--logback日志组件,Spring框架默认集成-->
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.2.3</version>
            </dependency>
    <!--        事务组件-->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.9.5</version>
            </dependency>
        </dependencies>

    12.设置applicationContext.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mv="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 标签作用
        在Spring IOC初始化过程中,自动创建并管理com.imooc.springmvc及子包中
        拥有以下注解的对象.
        @Repository
        @Service
        @Controller
        @Component
        -->
        <context:component-scan base-package="com.item.springmvc"/>
        <!--    启用spring mvc注解开发模式-->
        <mvc:annotation-driven>
            <!--        解决中文乱码问题-->
            <mvc:message-converters>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <!-- response.setContentType("text/html;charset=utf-8") -->
                            <value>text/html;charset=utf-8</value>
                            <value>application/json;charset=utf-8</value>
                        </list>
                    </property>
                </bean>
            </mvc:message-converters>
        </mvc:annotation-driven>
        <mvc:default-servlet-handler/>
    
        <!-- 将图片/JS/CSS等静态资源排除在外,可提高执行效率 -->
        <mvc:default-servlet-handler/>
    
    </beans>

    13.配置webappWEB-INFweb.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">
        <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:applicationContext.xml</param-value>
            </init-param>
            <load-on-startup>0</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
        <filter>
            <filter-name>characterFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>characterFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <filter>
            <filter-name>formContentFilter</filter-name>
            <filter-class>org.springframework.web.filter.FormContentFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>formContentFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    </web-app>

     14.添加一个实体

    public class Person {
        private String name;
        private Integer age;
    
        private Date birthday;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public Date getBirthday() {
            return birthday;
        }
    
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
    }
    View Code

    15.添加一个测试控制器

    @RestController
    @RequestMapping("/restful")
    public class RestfulController {
        @GetMapping("/request")
        public String doGetRequest(){
            return "{"message":"返回查询结果"}";
        }
    
        // POST /article/1
        // POST /restful/request/100
        @PostMapping("/request/{rid}")
        public String doPostRequest(@PathVariable("rid") Integer requestId, Person person){
            System.out.println(person.getName() + ":" + person.getAge());
            return "{"message":"数据新建成功","id":" + requestId + "}";
        }
    
        @PutMapping("/request")
        public String doPutRequest(Person person){
            System.out.println(person.getName() + ":" + person.getAge());
            return "{"message":"数据更新成功"}";
        }
    
        @DeleteMapping("/request")
        public String doDeleteRequest(){
            return "{"message":"数据删除成功"}";
        }
    
        @GetMapping("/person")
        public Person findByPersonId(Integer id){
            Person p = new Person();
            if(id==1){
                p.setName("lily");
                p.setAge(23);
            }else if(id==2){
                p.setName("smith");
                p.setAge(22);
            }
            return p;
        }
    
        @GetMapping("/persons")
        public List<Person> findPersons(){
            List list = new ArrayList();
            Person p1 = new Person();
            p1.setName("lily");
            p1.setAge(23);
            p1.setBirthday(new Date());
    
            Person p2 = new Person();
            p2.setName("smith");
            p2.setAge(22);
            p2.setBirthday(new Date());
            list.add(p1);
            list.add(p2);
            return list;
        }
    
    }

    16.前端请求

  • 相关阅读:
    Java生鲜电商平台-生鲜电商数据分析思维以及指标(小程序/APP)
    Java生鲜电商平台-生鲜电商中售后退款流程整理与架构实现(小程序/APP)
    Java生鲜电商平台-优惠券功能设计与开发(小程序/APP)
    Java生鲜电商平台-SpringCloud微服务架构中分布式事务解决方案
    Java生鲜电商平台-深入理解微服务SpringCloud各个组件的关联与架构
    Java生鲜电商平台-系统架构图
    Java生鲜电商平台-生鲜电商中微服务架构与集群模式如何选择?(小程序/APP)
    Java生鲜电商平台-生鲜电商微服务积分商城系统架构设计(小程序/APP)
    Java生鲜电商平台-微服务电商优惠券的架构设计(小程序/APP)
    Java生鲜电商平台-生鲜电商中优惠券的设计 (小程序/APP)
  • 原文地址:https://www.cnblogs.com/1439107348s/p/14312177.html
Copyright © 2011-2022 走看看