Spring boot starts the web, embedded tomcat, springtomcat

Source: Internet
Author: User

Spring boot starts the web, embedded tomcat, springtomcat

1. Configure the pom. xml file (as appropriate)

<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>


2. entity class User

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;}@Overridepublic 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;}@Overridepublic int hashCode() {return id != null ? id.hashCode() : 0;}}

3. Controller

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;}}

4. spring-boot (tomcat startup)

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// @SpringBootApplicationpublic 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);}}

Note: Most @ CompoentScan on the Internet does not have the following value. If no value is added, make sure that all programs are in the same package. Otherwise, 404 is displayed.

LZ also found this problem after several hours, so it was quite complete to avoid people making the same mistake again!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.