SpringBoot的简单应用以及部署
1. 项目目录结构
要部署在自己的Tomcat中的时候需要添加Java EE,或者是J2EE依赖包。否则在Application类中继承SpringBootServletInitializer的时候会报错。
2. java build path 中的内容:
注意事项:将页面底端的Default output folder 改成如图所示的样子。并将main/java , main/resources 的Output folder 改为 default ouput folder,并且将这两项的Include,Exclude项中的内容通过右边的Remove删掉。
3. pom.xml文件内容:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sucsoft</groupId>
<artifactId>SpringBootTest</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootTest</name>
<!-- Inherit defaults from spring boots -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.6.RELEASE</version>
</parent>
<!-- A typical dependencies of a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--如果要部署到自己的tomcat中,这一项配置必不可少,否则生成的war文件将无法执行。 如果不用部署到自己的Tomcat中,这一个依赖可以去掉-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!--在用maven 编译,打包过程中回出现javax.servlet找不到的情况,所以需要在这里配置-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<!-- The spring-boot-devtools module can be included in any project to
provide additional development-time features -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4. 类说明
package com.sucsoft;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
/**
* 如果要发布到自己的Tomcat中的时候,需要继承SpringBootServletInitializer类,并且增加如下的configure方法。
* 如果不发布到自己的Tomcat中的时候,就无需上述的步骤
*/
protected SpringApplicationBuilder configure(
SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
测试用的Controller类
package com.sucsoft.controller;
import java.util.Date;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sucsoft.entity.User;
@RestController
@RequestMapping("/user")
public class UserController {
@RequestMapping("/register")
public User register() {
User user = new User();
user.setName("hello");
user.setSex("female");
user.setBirthday(new Date());
return user;
}
}
测试用的实体类:
package com.sucsoft.entity;
import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
/**
* 用户
* @author Amei
*/
public class User {
private String id;
private String name;
private String sex;private Date birthday;
private String phone;
private String email;
private String address;
private String profileImageUrl;
private String description;
private Date registerDate;public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getProfileImageUrl() {
return profileImageUrl;
}
public void setProfileImageUrl(String profileImageUrl) {
this.profileImageUrl = profileImageUrl;
}
public Date getRegisterDate() {
return registerDate;
}
public void setRegisterDate(Date registerDate) {
this.registerDate = registerDate;
}public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", sex=" + sex
+ ", birthday=" + birthday + ", phone=" + phone + ", email="
+ email + ", address=" + address + ", profileImageUrl="
+ profileImageUrl + ", registerDate=" + registerDate + "]";
}
}
5. 测试
5.1使用内嵌的Tomcat测试:
进入项目的根目录,输入mvn spring-boot:run,开启项目。
在浏览器中输入localhost:8080/user/register 。 结果如下
5.2 使用发布到自己的Tomcat测试:
先进入项目根目录,执行 mvn package ,将项目打包为war包。
然后将target 目录中的SpringBootTest-0.0.1-SNAPSHOT.war文件复制到你所安装的tomcat目录中webapps目录中。然后去掉工程后边的后缀。
然后开启Tomcat,并在浏览器中输入: http://localhost:8080/SpringBootTest/user/register
5.3 通过MyEclipse直接发布到自己的Tomcat中。这种方法同发布普通的Web项目一样,这里不做详细说明。