spring boot 入门Demo

2017-06-18

147

0

技术:spring boot

运行环境:IDEA 15.2 + jdk8 + windows 10 + spring boot

demo功能:提供一个spring boot restful api基本 demo

建立空maven项目,导入maven依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.3.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <!-- 热部署 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>springloaded</artifactId>
        <version>1.2.5.RELEASE</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

建立 application.properties 文件

server.port=1000

项目结构

创建入口类, 从这里启动

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

/**
 * Created by francis on 2017/6/14.
 */
@SpringBootApplication
public class Startup extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Startup.class);
    }

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

创建api controller

import com.demoworld.common.IndexResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by francis on 2017/6/18.
 */

@Controller
@ResponseBody
public class HomeController {
    //没有参数直接返回json
    @RequestMapping("/")
    public IndexResult hi1() {
        IndexResult tmpResult = new IndexResult();
        tmpResult.setId(System.currentTimeMillis());
        tmpResult.setName("francis");
        tmpResult.setPassword("oiweohjfwe0921348021");
        return tmpResult;
    }

    //接收参数, 返回json
    @RequestMapping("/2")
    public IndexResult hi2(@RequestParam(value = "id", required = false, defaultValue = "0") long id) {
        IndexResult tmpResult = new IndexResult();
        tmpResult.setId(id > 0 ? id : System.currentTimeMillis());
        tmpResult.setName("francis");
        tmpResult.setPassword("oiweohjfwe0921348021");
        return tmpResult;
    }

    //接收数组, 返回json
    @RequestMapping("/3")
    public IndexResult hi3(@RequestParam(value = "ids", required = true, defaultValue = "") long[] ids) {
        IndexResult tmpResult = new IndexResult();
        tmpResult.setId(ids != null && ids.length > 0 ? ids[0] : System.currentTimeMillis());
        tmpResult.setName("francis");
        tmpResult.setPassword("oiweohjfwe0921348021");
        return tmpResult;
    }

    //接收参数对象, 返回json
    @RequestMapping("/4")
    public IndexResult hi4(@ModelAttribute("input") IndexResult input) {
        if (input != null && input.getId()>0) {
            return input;
        }
        IndexResult tmpResult = new IndexResult();
        tmpResult.setId(System.currentTimeMillis());
        tmpResult.setName("francis");
        tmpResult.setPassword("oiweohjfwe0921348021");


        return tmpResult;
    }
}

依次访问, 啥意思呢,看代码吧

1. 访问第一个api http://localhost:1000/

2. 访问第二个api http://localhost:1000/2

3. 访问第三个api http://localhost:1000/3?ids=1,2,3,4

4. 访问第三个api http://localhost:1000/3

5. 访问第四个api http://localhost:1000/4 

6. 访问第四个api http://localhost:1000/4?id=123&name=francis&password=sdofisodfi 根据参数参数绑定,再输出

欢迎添加微信,互相学习↑↑↑ -_-

发表评论

全部评论:0条

白老虎

programming is not only to solve problems, ways to think