SpringBoot_01
一、安装Spring Tools Suite(For Eclipse)
地址 http://www.roncoo.com/article/detail/124661
二、使用插件建立SpringBoot工程,与简单测试
1. File -> new -> Other... -> 查找Spring Starter Project 确定 (springboot 基于Maven 所以创建完demo 后报错,可能是maven 的版本有问题)
2. 实例
package com.errsr.bean;
public class User {
private String name;
private String age;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
package com.errsr.controller;
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 com.errsr.bean.User;
@RestController
@RequestMapping
public class IndexController {
@RequestMapping(value = "index")
private String index() {
System.out.println("Hello Word!");
return "hello Spring Boot!";
}
@RequestMapping(value = "get")
private Map<String, String> get(@RequestParam String name) {
Map<String, String> map = new HashMap<String, String>();
map.put("name", name);
map.put("value", "hello Spring boot");
return map;
}
@RequestMapping(value = "find/{name}/{age}")
private User find(@PathVariable String name,@PathVariable String age) {
User user = new User();
user.setName(name);
user.setAge(age);
user.setSex("男");
return user;
}
}
运行测试:
package com.errsr;
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;
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.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.errsr.controller.IndexController;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootRestFullTestApplicationTests {
private MockMvc mvc;
@Before
public void before(){
this.mvc = MockMvcBuilders.standaloneSetup(new IndexController()).build();
}
@Test
public void contextLoads() throws Exception {
RequestBuilder req = get("/index");
ResultActions isOk = mvc.perform(req).andExpect(status().isOk());
isOk.andExpect(content().string("hello Spring Boot!"));
}
}
附:如何在Eclipse中直接打开本地目录 自寻

浙公网安备 33010602011771号