移动大脑-SpringMVc搭建RestFul后台服务(一)-环境搭建
来源:互联网 发布:四旋翼飞控编程 编辑:程序博客网 时间:2024/05/18 00:38
目录:
移动大脑-SpringMVc搭建RestFul后台服务(一)-环境搭建
移动大脑-SpringMVc搭建RestFul后台服务(二)-配置mysql数据库
移动大脑-SpringMVc搭建RestFul后台服务(三)-RestFul接口编写(模拟用户注册登录)
移动大脑-SpringMVc搭建RestFul后台服务(四)-添加Token过滤器
移动大脑-SpringMVc搭建RestFul后台服务(五)-支付宝支付
移动大脑-SpringMVc搭建RestFul后台服务(六)-微信支付(Android)
移动大脑-SpringMVc搭建RestFul后台服务(七)-增量更新
话说作为一个移动端的程序猿来写后端服务程序是不是很不专业!其实不然,虽说不能达到很好的性能和很好的健壮性,但对于理解整体的业务逻辑是很有帮助的;再者如果要开发一个简单的有后端有前端的移动APP来说,一个人也能完成所有工作,还是挺好的。这里就给大家介绍怎样用Spring mvc来搭建RestFul类型的后端服务。
还是先看看这篇文章要达到的效果:
访问URL:http://localhost:8080/user/loginByPwd.do?username=ywl5320&password=123456(这里为了方便测试,使用的是get方法,正式项目中应该用post方法)
效果图如下:
这就是一个简单的restful类型的后端服务程序了。那么我们开始搭建吧!
一、环境搭建
1.1、安装Tomact
网上很多相关的教程,这里就不介绍了,如果是在不会也可以问我。
1.2、安装Intellij idea
下载地址(https://www.jetbrains.com/idea/download/),破解也问问度娘吧,很容易的。
二、创建项目
2.1、创建空的maven项目
2.2、填写GroupId(可填包名)和Artifactld(可填项目名称)
2.3、填写项目名称和存储路径完成项目创建
二、添加Spring支持
上一步只是创建了maven的一个空项目,现在需要添加Spring的支持,使之成为一个web项目。
这样就把为项目添加了Spring的支持了。
三、通过maven添加Spring的库和一下基础库(json,日志等)
在文件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.ywl5320.appservice</groupId> <artifactId>AppService</artifactId> <version>1.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.2.6.RELEASE</spring.version> <hibernate.version>5.0.12.Final</hibernate.version> <jackson.version>2.5.0</jackson.version> </properties> <dependencies> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <!-- 使用SpringMVC需配置 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- json --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <!-- servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies></project>
添加完后,并构建一下项目才能右键添加spring相关的文件。
四、在resources中添加beans.xml、springmvc.xml和log4j.properties4.1、添加beans.xml
beans.xml文件内容如下:spring自动扫描制定包名下的所有注解
<?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" 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-4.2.xsd"> <context:component-scan base-package="com.ywl5320.appservice" use-default-filters="false"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> </context:component-scan></beans>springmvc.xml如下:添加servlet和注解驱动
<?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" 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-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> <context:component-scan base-package="com.ywl5320.appservice"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> </context:component-scan> <mvc:default-servlet-handler/> <mvc:annotation-driven/></beans>log4j.properties内容如下:直接复制即可
log4j.rootLogger=WARN, stdout, R log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout # Pattern to output the caller's file name and line number.#log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n# Print the date in ISO 8601 formatlog4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=example.log log4j.appender.R.MaxFileSize=100KB # Keep one backup filelog4j.appender.R.MaxBackupIndex=1 log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n # Print only messages of level WARN or above in the package com.foo.log4j.logger.com.foo=WARN五、配置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"> <!-- 指定Spring 实体类(Bean)的配置文件所在目录,默认配置在WEB-INF目录下,这里我们resources里面的,管理更方便--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- The front controller of this Spring Web application, responsible for handling all application requests --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.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>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置 HiddenHttpMethodFilter: 把 POST 请求转为 DELETE、PUT 请求 --> <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></web-app>web.xml中主要配置Spring的contextConfigLocation、listener、servlet和RestFul配置(post转成delete和put)
这样环境就搭建完成了。
六、最后创建测试文件(这里只用了action层,dao和service暂时没用)
6.1、创建返回给移动端的实体类
FestFulBean.java
package com.ywl5320.appservice.bean;/** * Created by ywl on 2017-10-2. */public class FestFulBean<T> { private int status; private T data; private String msg; public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public T getData() { return data; } public void setData(T data) { this.data = data; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; }}UserAction.java
package com.ywl5320.appservice.action;import com.ywl5320.appservice.bean.FestFulBean;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;/** * Created by ywl on 2017-10-2. */@Controller@RequestMapping("/user")public class UserAction { @ResponseBody @RequestMapping(value="/loginByPwd.do", method= RequestMethod.GET) public FestFulBean<String> loginByPwd(@RequestParam String username, @RequestParam String password) { FestFulBean<String> restful = new FestFulBean<String>(); restful.setData("hello, " + username + " welcom to my website!"); restful.setStatus(0); restful.setMsg("成功"); return restful; }}其中的注解都是Spring中的基础,不知道可以去看看相关的介绍,知道意思了就会觉得很简单了。
七、配置war和tomact
7.1、配置war包
点击File打开Project Structure
7.2、选择Artifacts,并添加相关库到war中,如图:
7.3、配置tomact
7.3.1、打开tomact配置窗口
7.3.2、点击添加tomact
7.3.3、填写tomact名称并添加7.2中创建的war文件,如图,点击右下角fix可自动添加
八、发布项目并访问
点击tomact叛变的运行按钮即可,发布成功左下边会显示OK状态
这样一个简单的服务器雏形就搭建好了,后面还会慢慢的添加诸如数据库、拦截器等等功能,好了最后祝大家国庆中秋双节快乐阖家欢乐!
源码下载:GitHub(AppServiceRestFul)
- 移动大脑-SpringMVc搭建RestFul后台服务(一)-环境搭建
- 移动大脑-SpringMVc搭建RestFul后台服务(六)-微信支付(Android)
- 移动大脑-SpringMVc搭建RestFul后台服务(二)-配置mysql数据库
- 移动大脑-SpringMVc搭建RestFul后台服务(四)-添加Token过滤器
- 移动大脑-SpringMVc搭建RestFul后台服务(五)-支付宝支付
- 移动大脑-SpringMVc搭建RestFul后台服务(七)-增量更新
- 移动大脑-SpringMVc搭建RestFul后台服务(三)-RestFul接口编写(模拟用户注册登录)
- Restful后台系统搭建(一)
- springmvc下Jersey 搭建RESTful服务
- springMVC教程(一) 搭建环境
- SpringMVC总结(一)搭建环境
- SpringMVC基础(一)开发环境搭建
- Spring+SpringMVC环境搭建(一)
- SpringMVC学习之环境搭建(一)
- Restful后台系统搭建(二)
- Restful后台系统搭建(三)
- Restful后台系统搭建(四)
- (后台学习阶段)SpringMvc(一)Eclipse搭建web项目
- LeetCode:Find Largest Value in Each Tree Row
- python shelve模块的用法
- hdu 4498 自适应simpson
- WebRTC视频帧渲染前处理——等比例填充显示窗口
- 滴滴出行之地下迷宫
- 移动大脑-SpringMVc搭建RestFul后台服务(一)-环境搭建
- 第四章 对象与类
- 学习记录7(17/10/02 于隆昌)
- HTML 注释 水平线
- GDB调试器&Make工程管理器
- kk《必然》后感之'知化'
- java web 使用fliter定义权限拦截(例如:提交订单,我的订单 没有登录前不允许访问)
- 【笔记】欧拉计划第一题
- opencv学习笔记(四)