皮杜妮

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  887 Posts :: 3 Stories :: 7 Comments :: 0 Trackbacks
spring boot 是spring 4.0提供的微框架,支持 jdk 1.8, maven 3以上,  否则会报一些错误。

1. pom 文件, 主要写依赖关系,
<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. 然后就是提供的接口类UserController, 实体类User, 主要是几个注解
@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.  然后就是程序启动,这里不需要spring xml文件,完全依赖注解。
@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"}






posted on 2016-05-04 15:39 猪眼睛 阅读(178) 评论(0)  编辑  收藏 所属分类: Spring