Spring boot web, inline tomcat

Source: Internet
Author: User

1.pom.xml file configuration (modify 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 FAL Se 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 controllers

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

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 net do not have the following value, if not added must ensure that all the programs in a package, otherwise it will appear 404.

LZ also turned over for several hours only to find that this problem, so wrote a relatively complete, to avoid the same mistakes to make again!


Spring boot web, inline tomcat

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.