标签:spring boot tomcat-embedded 内嵌tomcat java启动tomcat spring
1.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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.nnk.maven.test</groupId> <artifactId>test-sample</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <java.version>1.7</java.version> <start-class>com.nnk.maven.test.boot.Application</start-class> </properties> <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.0.1.RELEASE</version> </parent> <!-- Add typical dependencies for a web application --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </pluginManagement> </build> <repositories> <repository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </pluginRepository> </pluginRepositories> </project>
package com.nnk.maven.test.entity; /** * */ public class User { private Long id; private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; if (id != null ? !id.equals(user.id) : user.id != null) return false; return true; } @Override public int hashCode() { return id != null ? id.hashCode() : 0; } }
package com.nnk.maven.test.controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.nnk.maven.test.entity.User; /** * */ @RestController @RequestMapping("/user") public class UserController { @RequestMapping("/{id}") public User view(@PathVariable("id") Long id) { User user = new User(); user.setId(id); user.setName("zhang"); return user; } }
package com.nnk.maven.test.boot; import java.util.HashSet; import java.util.Set; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; /** * */ @Configuration @ComponentScan("com.nnk") @EnableAutoConfiguration // @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication app = new SpringApplication(Application.class); app.setWebEnvironment(true); app.setShowBanner(false); Set<Object> set = new HashSet<Object>(); // set.add("classpath:applicationContext.xml"); app.setSources(set); app.run(args); } }
LZ 也是翻了好几个小时才发现是这个问题,所以写了一个比较完整的,避免来人再犯同样的错误!
标签:spring boot tomcat-embedded 内嵌tomcat java启动tomcat spring
原文地址:http://blog.csdn.net/wlz520520/article/details/45563501