cfx集成spring mvc

本文记录了一次将老旧的JAX-RS接口迁移至Spring MVC的过程,包括添加相关Maven依赖、配置Spring MVC基础设置及web.xml,以实现更简便的接口开发。通过这次迁移,开发效率得到了显著提升。

前言

由于公司项目年代久远还在使用JAX-RS框架写rest接口,平时开发使用起来并不方便,例如获取request对象步骤繁琐,上传文件还需要添加依赖,新模块的接口还需要写配置rs.xml等等;中途简单尝试过两次都失败了,在某次新开excel导入接口,写上传文件写到崩溃,下定决心进行集成。

MAVEN依赖:

<!--spring 4.3.13-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
</dependency>

<!--spring-mvc cfx 兼容依赖-->
<dependency>
  <groupId>joda-time</groupId>
  <artifactId>joda-time</artifactId>
  <version>2.10.5</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.jaxrs</groupId>
  <artifactId>jackson-jaxrs-json-provider</artifactId>
  <version>2.8.7</version>
</dependency>

spring mvc基础配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <!-- 配置自动扫描包 -->
    <context:component-scan base-package="com.demo"/>
    <!-- 开启注解 -->
    <mvc:annotation-driven/>

web.xml配置:

    <!-- 配置springmvc 的DispatcherServlet -->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

以上配置就已完成

现在开始测试:

mvc实例
spring-mvc简单实用

终于进入工业时代2.0了……待续……

其他文章:

SpringMVC配置详解 Spring MVC配置详解_今天学什么?的博客-优快云博客

SpringJpa踩坑日记 SpringJpa踩坑日记之自动更新_今天学什么?的博客-优快云博客

一文解决MAVEN依赖冲突 一文解决所有maven jar包依赖冲突!_今天学什么?的博客-优快云博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值