SpringBoot IntelliJ建立簡單的Restful接口

使用SpringBoot快速建服務,和NodeJS使用express幾乎如出一轍,主要分爲如下:java

1.添加和安裝依賴  2.添加路由(即接口) 3.對路由事件進行處理git

一樣坑的地方就是,祖國的防火牆太過強大,必需要把maven換成國內鏡像源,阿里雲的速度就很是快。github

 

 

1.經過spring initializr建立項目web

2.填好工程名,一路next到底spring

3.新建controller的package,新建class "DemoServer",填入如下內容:express

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.ResponseBody;

@RestController
@RequestMapping(value = "/index")
public class NewServer {
    @RequestMapping
    public String index() {
        return "spring boot server";
    }

    // @RequestParam 簡單類型的綁定,能夠出來get和post
    @RequestMapping(value = "/get")
    public HashMap<String, Object> get(@RequestParam String name) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("title", "hello world");
        map.put("name", name);
        return map;
    }
    @RequestMapping("/json")
    @ResponseBody
    public Map<String,Object> json(){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("name","Ryan");
        map.put("age","18");
        map.put("sex","man");
        return map;
    }
}

4.會提示缺乏 org.springframework.web的依賴,在pom.xml中添加該依賴:json

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
	</dependencies>

 這裏有個IntelliJ很是傻bi的bug,你不去修改pom.xml,他默認居然不會安裝裏面的依賴。app

 因此你須要,先剪切<dependencies>標籤裏的內容,保存。而後從新複製進去,再保存,而後就會自動添加依賴了。。。真他媽簡直了。。maven

 

   若是IntelliJ安裝依賴有問題,能夠經過cd到工程目錄,使用mvn install進行安裝。spring-boot

 

5.新建model的pakcage,寫一個model類User

package com.example.model;

/**
 * Created by shenzw on 31/10/2016.
 */
import java.util.Date;

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

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

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}

  

經過在DemoServer中添加路由,能夠在路徑中獲取參數,而後拼裝一個User對象,快速序列化成JSON返回

// @PathVariable 得到請求url中的動態參數
    @RequestMapping(value = "/get/{id}/{name}")
    public User getUser(@PathVariable int id, @PathVariable String name) {
        User user = new User();
        user.setId(id);
        user.setName(name);
        user.setDate(new Date());
        return user;
    }

  

6.點擊運行,或選擇DemoServer這個Class右鍵RUN

http://localhost:8080/index  能夠訪問到這個服務

http://localhost:8080/index/json 會返回json

http://localhost:8080/index/get?name=123 能夠進行GET請求獲取

http://localhost:8080/index/get/123/xiaoming 能夠在路徑中傳遞參數

 

demo:

https://github.com/rayshen/spring-boot-demo/tree/master/spring-boot-demo-2-1

相關文章
相關標籤/搜索