环境
开发环境
- Windows7旗舰版
- eclipse 版本
Eclipse Java EE IDE for Web Developers.
Version: Luna Service Release 2 (4.4.2)
部署环境
- tomcat 版本
tomcat 7.0.65 - Linux 环境
Centos7 64 位 - Java版本
java version “1.7.0_79”
开发
目标:用spring 支持 restful
项目目录
- springREST
- src
- demo
- Demo.java
- demo
- WebRoot
- WEB-INF
- web.xml
- rest-servlet.xml
- WEB-INF
- src
项目文件
- web.xml 配置spring支持
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>SpringREST</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/rest-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- rest-servlet.xml 配置rest支持
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-lazy-init="true">
<context:component-scan base-package="demo" />
</beans>
- java代码 Demo.java
package com.liqiu.controller;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/demo")
public class Demo{
@RequestMapping(value = "/{id}")
public void get(@PathVariable String id,HttpServletResponse response) throws IOException {
response.getWriter().write(id);
}
}
项目部署
- 导出war文件
tomcat 安装
- 下载tomcat core linux 版
- 上传并解压
tar -zxvf apache-tomcat-7.0.65.tar.gz
mv apache-tomcat-7.0.65 /opt/tomcat 配置tomcat
- 在 TOMCAT_HOME/bin/catalina.sh 中加入java环境变量
export JAVA_HOME=/usr/java/jdk1.7.0_79 export CLASSPATH=.:$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
配置tomcat 用户
修改 TOMCAT_HOME/conf/tomcat-users.xml<tomcat-users> <role rolename="admin"/> <role rolename="manager-script"/> <role rolename="manager-gui"/> <role rolename="manager-jmx"/> <role rolename="manager-status"/> <role rolename="admin-gui"/> <role rolename="admin-script"/> <user username="admin" password="admin" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-gui,admin-script"/> </tomcat-users>
配置tomcat环境变量 修改 /etc/profile
export TOMCAT_HOME=/usr/local/tomcat export PATH=$PATH:$TOMCAT_HOME/bin
- 在 TOMCAT_HOME/bin/catalina.sh 中加入java环境变量
- 启动tomcat
startup.sh - 关闭tomcat
shutdown.sh - 登陆tomcat进行部署
- 访问 http://you_server:8080/
- 点击Manage APP 用刚才配置的用户名(admin)密码(admin)登陆
- 上传war文件 点击部署,即可完成部署
验证效果
访问 http://you_server:8080/SpringREST/rest/hello world
效果:
hello world
注意事项
- 开发环境和部署环境java版本应一致
- 如果tomcat能正常开启但是无法访问,通常是因为防火墙原因
关闭防火前:systemctl stop firewalld.service
检查是否关闭:systemctl status firewalld.service
- 如果tomcat启动报各种奇怪的错误,可能是因为web项目开发缺少相应的jar包,或者tomcat和spring某些版本jar包有冲突,可尝试更换jar包。