第一个Spring restful 项目-从开发到部署

环境

开发环境

  • 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
    • WebRoot
      • WEB-INF
        • web.xml
        • rest-servlet.xml

项目文件

  • 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文件
    导出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

      java环境便令

      • 配置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
    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包。
restful restful所需要的jar包 ========================================= Restlet, a RESTful Web framework for Java ========================================= http://www.restlet.org ----------------------------------------- Native REST support * Core REST concepts have equivalent Java classes (UniformInterface, Resource, Representation, Connector for example). * Suitable for both client-side and server-side web applications. The innovation is that that it uses the same API, reducing the learning curve and the software footprint. * Restlet-GWT module available, letting you leverage the Restlet API from within any Web browser, without plugins. * Concept of "URIs as UI" supported based on the URI Templates standard. This results in a very flexible yet simple routing with automatic extraction of URI variables into request attributes. * Tunneling service lets browsers issue any HTTP method (PUT, DELETE, MOVE, etc.) through a simple HTTP POST. This service is transparent for Restlet applications. Complete Web Server * Static file serving similar to Apache HTTP Server, with metadata association based on file extensions. * Transparent content negotiation based on client preferences. * Conditional requests automatically supported for resources. * Remote edition of files based on PUT and DELETE methods (aka mini-WebDAV mode). * Decoder service transparently decodes compressed or encoded input representations. This service is transparent for Restlet applications. * Log service writes all accesses to your applications in a standard Web log file. The log format follows the W3C Extended Log File Format and is fully customizable. * Powerful URI based redirection support similar to Apache Rewrite module. Available Connectors * Multiple server HTTP connectors available, based on either Mortbay's Jetty or the Simple framework or Grizzly NIO framework. * AJP server connector available to let you plug behind an Apache HTT
课程简介这是一门使用Java语言,SpringBoot框架,从0开发一个RESTful API应用,接近企业级的项目(我的云音乐),课程包含了基础内容,高级内容,项目封装,项目重构等知识,99%代码为手写;因为这是项目课程;所以不会深入到源码讲解某个知识点,以及原理,但会粗略的讲解下基础原理;主要是讲解如何使用系统功能,流行的第三方框架,第三方服务,完成接近企业级项目,目的是让大家,学到真正的企业级项目开发技术。适用人群刚刚毕业的学生想提高职场竞争力想学从零开发SpringBoot项目想提升SpringBoot项目开发技术想学习SpringBoot项目架构技术想学习企业级项目开发技术就是想学习SpringBoot开发能学到什么从0开发一个类似企业级项目学会能做出市面上90%通用API快速增加1到2年实际开发经验刚毕业学完后能找到满意的工作已经工作学完后最高涨薪30%课程信息全课程目前是82章,155小时,每节视频都经过精心剪辑。在线学习分辨率最高1080P课程知识点1~11章:学习方法,项目架构,编码规范,Postman使用方法,Git和Github版本控制12~16章:搭建开发环境,快速入门SpringBoot框架17~20章:快速入门MySQL数据库21~30章:MyBatis,登录注册,找回密码,发送短信,发送邮件,企业级接口配置31~41章:实现歌单,歌单标签,音乐,列表分页,视频,评论,好友功能42~48章:阿里云OSS,话题,MyBatis-plus,应用监控49~53章:Redis使用,集成Redis,SpringCache,HTTP缓存54~58章:Elasticsearch使用,集成Elasticsearch,使用ES搜索59~61章:商城,集成支付宝SDK,支付宝支付62~64章:常用哈希和加密算法,接口加密和签名65~67章:实时挤掉用户,企业级项目测试环境,企业级接口文档68~69章:SpringBoot全站HTTPS,自签证书,申请免费证书70~73章:云MySQL数据库,云Redis数据库使用,轻量级应用部署环境,域名解析74~80章:Docker使用,生产级Kubernetes集群,域名解析,集群全站HTTPS81~82章:增强和重构项目,课程总结,后续学习计划
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值