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項目一樣,這里不做詳細說明。