<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.example</groupId>
<artifactId>spring-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-example</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.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>
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/libs-snapshot</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/libs-snapshot</url>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. 鐒跺悗灝辨槸鎻愪緵鐨勬帴鍙g被UserController錛?瀹炰綋綾籙ser錛?涓昏鏄嚑涓敞瑙?/span>
@RestController l綾誨悓spring mvc 鐨凘Controller
@RequestMapping 綾誨悓spring mvc
package two;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
@RequestMapping("/hello")
public User view(){
User user=new User();
user.setId((long)100);
user.setName("fanjs");
return user;
}
package two;
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;
}
3. 鐒跺悗灝辨槸紼嬪簭鍚姩錛岃繖閲屼笉闇瑕乻pring xml鏂囦歡錛屽畬鍏ㄤ緷璧栨敞瑙c?/span>
@EnableAutoConfiguration
@Configuration
@ComponentScan
package two;
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
@EnableAutoConfiguration
public class RunMain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(RunMain.class, args);
}
}
4. 嫻嬭瘯璺緞
http://localhost:8080/user/hello
{"id":100,"name":"fanjs"}