maven构建Spring Boot

1.新建一个maven web项目

2.配置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>SpringBoot</groupId>
<artifactId>testSpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBoot</name>
<packaging>jar</packaging>


<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.5.BUILD-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>


<!--maven的插件 -->
<build>
<!-- 配置java版本 不配置的话默认父类配置的是1.8 -->
<pluginManagement>
<plugins>


<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>


<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>




</plugins>
</pluginManagement>
</build>
</project>

3.写一个实体类User

package com.yc.springboot.bean;


public class User {
private int id;
private String name;

@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}


public int getId() {
return id;
}


public void setId(int id) {
this.id = id;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}


@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}


public User(int id, String name) {
super();
this.id = id;
this.name = name;
}


public User() {
super();
}
}


4.写一个控制器类

package com.yc.springboot.controller;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.yc.springboot.bean.User;


@Controller
//开启自动配置,然后通过SpringApplication.run(UserController.class);
//运行这个控制器;这种方式只运行一个控制器比较方便;
@EnableAutoConfiguration
public class HelloController {
@RequestMapping(value="/get/{id}/{name}")
@ResponseBody
public User getUser(@PathVariable int id,@PathVariable String name){
User user=new User();
user.setId(id);
user.setName(name);
return user;
}

@RequestMapping("/hello")
@ResponseBody
String hello() {
System.out.println("进来了...");
return "Hello World!";
}

public static void main(String[] args) {
SpringApplication.run(HelloController.class, args);  
}

//运行方式:在项目右键->Run As->Java Application->选择这个类即可
//访问方式 http://127.0.0.1:8080/hello
}


5.在项目上右击选择->Run As-> Java Application



6.选择含有main方法的那个类,如下图所示:



7.出现如下图所以,说明部署成功。



8.远程访问,输入http://127.0.0.1:8080/hello,输出Hello World!
          输入http://127.0.0.1:8080/get/1001/yc, 输出
{"id":1001,"name":"yc"}