版权声明:本站所提供的文章资讯、软件资源、素材源码等内容均为本作者提供、网友推荐、互联网整理而来(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考,如有侵犯您的版权,请联系我,本作者将在三个工作日内改正。 https://blog.csdn.net/weixin_42323802/article/details/83964225
声明,使用 maven3.5.4,springboot2.0,JDK8 ,idea2018.2
模块目录结构:
main 主方法:
@SpringBootApplication
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
model实体类:
/**
* @auther SyntacticSugar
* @data 2018/11/11 0011下午 10:03
*/
public class User {
private String name;
private int age;
private String pass;
// getter setter
.....
}
controller层,定义了单个对象,list集合,以及传参情况下的 get 、post请求;
package com.neo.springbootweb.web;
/**
* @auther SyntacticSugar
* @data 2018/11/11 0011下午 10:04
*/
// bug一次, @controller需要配合@requestbody
@RestController
public class WebController {
// 定义method 、 path
@RequestMapping(name = "/getUser", method = RequestMethod.POST)
public User getUser() {
User user = new User();
user.setAge(12);
user.setName("小明");
user.setPass("123456");
return user;
}
// getUsers 获取list 集合
@RequestMapping(value = "/getUsers",method = RequestMethod.POST)
public List<User> getUsers() {
ArrayList<User> users = new ArrayList<>();
User user1 = new User();
user1.setName("neo");
user1.setAge(30);
user1.setPass("neo123");
users.add(user1);
User user2 = new User();
user2.setName("小明");
user2.setAge(12);
user2.setPass("123456");
users.add(user2);
return users;
}
/**
* 以下两种方式都可以;@GetMapping("get/{name}") 简化写法
* @param name
* @return
*/
// @RequestMapping(value = "get/{name}",method = RequestMethod.GET)
@GetMapping("get/{name}")
public String getname(@PathVariable("name") String name ){
return name;
}
}
test方法,前两个单元测试 返回 json格式的字符串,controller层第三个方法测试,在浏览器 url 访问;http://localhost:8080/get/hello
返回hello说明成功:
package com.neo.springbootweb;
import com.neo.springbootweb.web.WebController;
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.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
@RunWith(SpringRunner.class)
@SpringBootTest
public class WebApplicationTests {
//MockMvcBuilders.standaloneSetup(...) 中的参数需要导入 controller的实例
private MockMvc mockMvc;
@Before
public void setUp(){
mockMvc= MockMvcBuilders.standaloneSetup(new WebController()).build();
}
/**
* .andReturn().getResponse().getContentAsString()
* 获取请求的返回信息
* @throws Exception
*/
@Test
public void getUser() throws Exception {
String contentAsString = mockMvc.perform(MockMvcRequestBuilders.post("/getUser")).andReturn().getResponse().getContentAsString();
System.out.println(contentAsString);
}
@Test
public void getUsers() throws Exception {
String contentAsString = mockMvc.perform(MockMvcRequestBuilders.post("/getUsers")).andReturn().getResponse().getContentAsString();
System.out.println(contentAsString);
}
}