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


發佈了70 篇原創文章 · 獲贊 78 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章