RESTful开发风格2:RESTful开发风格初体验二:创建Spring MVC工程;创建第一个遵循RESTful风格的后端案例代码;(本篇博客的主要目的是,先打个基础,做好准备)

本文档逐步指导如何建立一个基于maven的SpringMVC RESTful项目,涵盖从创建项目、配置环境到实现RESTful风格的简单案例。主要涉及SpringMVC的配置,包括pom.xml、web.xml和applicationContext.xml的配置内容,以及解决POST请求乱码问题。最后通过一个GET请求的RESTful控制器展示其实现,并预告后续将演示客户端与RESTful交互的过程。

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

说明:

(1)本篇博客通过一个简单的案例,开发第一个RESTful风格的项目;

(2)本篇博客没什么难点,只是创建一个项目,为后面的演示作准备;同时,初步的演示了RESTful开发风格的程序;

目录

一:准备一个项目;

1.创建一个【maven + WebApp】项目;

2.Spring MVC环境配置;

3.【pom.xml,web.xml,applicationContext.xml】,三个关键文件的内容; 

二:基于【RESTful风格】开发一个案例;


一:准备一个项目;

1.创建一个【maven + WebApp】项目;

本部分可以参考【SpringMVC入门与数据绑定2:Spring MVC初体验二:使用IDEA创建【maven + WebApp】项目;】;

2.Spring MVC环境配置;

本部分可以参考【SpringMVC入门与数据绑定3:Spring MVC初体验三:Spring MVC环境配置;(通过一个简单案例,走了一遍Spring MVC流程;)

3.【pom.xml,web.xml,applicationContext.xml】,三个关键文件的内容; 

其中的pom.xml、web.xml和applicationContext.xml的内容如下:

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.imooc</groupId>
    <artifactId>restful</artifactId>
    <version>1.0-SNAPSHOT</version>

    <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-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
    </dependencies>

</project>

说明:

(1)设置了国内阿里仓库;引入了【spring-webmvc】模块;


web.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_3_1.xsd"
         version="3.1">

    <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>

</web-app>

说明:

(1)配置了DispatcherServlet;配置了【CharacterEncodingFilter过滤】来解决POST请求的中文乱码问题;


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 base-package="com.imooc.restful"></context:component-scan>

    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=utf-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <mvc:default-servlet-handler/>

</beans>

说明:

(1)<context:component-scan>:开启组件扫描;<mvc:annotation-driven/>:启动Spring MVC开发模式;<mvc:default-servlet-handler/>:将静态资源排除; 

(2)通过【StringHttpMessageConverter】转换器,来解决响应的中文乱码问题;


二:基于【RESTful风格】开发一个案例;

RestfulController类:

package com.imooc.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\":\"客户端返回的查询结果\"}";
    }

}

………………………… 

声明:上面return的写错了,少了{}大括号;特此说明,下面的几张图就不改了;

  

…………………………

说明:

(1)上面就是一个标准的RESTful风格的程序;

(2)这段程序,我们以前也遇到过啊;为什么说,这就就是RESTful风格的程序了?:

一个老生常淡的注意项:启动Tomcat服务器前,记得设置Artifacts把新引入的jar包引入到发布中去;

启动Tomcat,观察结果:

说明:

(1)在这个过程中,遇到了【Servlet[springmvc]的Servlet.init()异常】,具体解决策略见【Servlet[springmvc]的Servlet.init()引发异常;】;

(2)上面后端返回的数据是【纯粹的JSON数据】,没有任何与展现相关的内容。

               ● 如果我们希望【后端返回的数据】被成功的显示,需要客户端的支持;

               ● 客户端可以是【一个HTML页面】,也可以是【一个App】,也可以是【微信小程序】;

(3)而,【HTML页面】、【App】、【微信小程序】这些不同的客户端,都提供了和url进行交互的功能;

               ● 【HTML页面】在通过url得到后端返回的JSON数据时:Ajax技术就HTML使用的技术;(Ajax以前也接触过很多次,自己也总结过);

下篇博客就通过【RESTful实验室】,来演示客户端页面和RESTful交互的过程;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值