您的位置:首页 > 编程语言 > Java开发

Spring Boot -- RESTFul API 简单项目的快速搭建

2018-03-05 23:36 856 查看

创建项目

参照 http://blog.csdn.net/oktfolio/article/details/79451766

sring-boot-starter-web 引入的依赖包



引入 spring-tool 依赖

修改代码,自动编译运行。

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>


需要修改 IntelliJ IDEA 的配置。

勾选 Build project automatically



Ctrl + Shift + Alt + /

Registry

勾选 compiler.automake.allow.when.app.running



运行

直接运行 main 方法、IntelliJ IDEA 运行,或者使用 maven 命令:spring-boot:run

Code

IndexController.java

package me.oktfolio.springboot.demo_2_1.controller;

import me.oktfolio.springboot.demo_2_1.bean.User;
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 java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@RestController // 包含 @ResponseBody
@RequestMapping(value = "/index")
public class IndexController {

@RequestMapping
public String index() {
return "hello world";
}

@RequestMapping(value = "/get")
public Map<String, String> get(@RequestParam String name) {
Map<String, String> map = new HashMap<>();
map.put("name", name);
map.put("value", "hello world");
return map;
}

@RequestMapping(value = "/find/{id}/{name}")
public User get(@PathVariable("id") Integer id, @PathVariable(value = "name") String name) {
User user = new User();
user.setId(id);
user.setUserName(name);
user.setLocalDateTime(LocalDateTime.now());
return user;
}
}


User.java

package me.oktfolio.springboot.demo_2_1.bean;

import java.time.LocalDateTime;

public class User {

private Integer id;
private String userName;

private LocalDateTime localDateTime;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public LocalDateTime getLocalDateTime() {
return localDateTime;
}

public void setLocalDateTime(LocalDateTime localDateTime) {
this.localDateTime = localDateTime;
}
}


测试类

package me.oktfolio.springboot.demo_2_1;

import me.oktfolio.springboot.demo_2_1.controller.IndexController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Demo21ApplicationTests {

private MockMvc mockmvc;

@Before
public void before() {
this.mockmvc = MockMvcBuilders.standaloneSetup(new IndexController()).build();
}

@Test
public void contextLoads() throws Exception {
RequestBuilder requestBuilder = get("/index");
mockmvc.perform(requestBuilder)
.andExpect(status().isOk())
.andExpect(content().string("hello world"));
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: