版权声明:版权没有,盗用不究 https://blog.csdn.net/liman65727/article/details/81949472
前言
这一篇总结一下spring boot中web模块中常用的东西。
构建Restful风格的简单CRUD
Restful风格,现在我还暂时解释不清楚,只能意会。具体参考这篇博客——restful风格介绍
URL | 请求类型 | 接口功能 |
---|---|---|
/users | GET | 查询用户列表 |
/users | POST | 创建一个用户 |
/users/id | GET | 根据id查询一个用户 |
/users/id | PUT | 根据id更新一个用户信息 |
/users/id | DELETE | 根据id删除一个用户信息 |
准备domain对象
package com.learn.springbootresuful.domain;
/**
*
* @author liman
* @createtime 2018年8月22日
* @contract 15528212893
* @comment:
*
*/
public class User {
private Long id;
private String name;
private Integer age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
完成controller
其中用到的一些注解需要注意的就是@ModelAttribute和@PathVariable即可,第一个是spring mvc中常用的模型视图对象标签。
package com.learn.springbootresuful.web;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.learn.springbootresuful.domain.User;
@RestController
@RequestMapping(value = "/users")
public class UserController {
static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>());
@RequestMapping(value = "/", method = RequestMethod.GET)
public List<User> getUserList() {
List<User> allUser = new ArrayList<User>(users.values());
return allUser;
}
@RequestMapping(value = "/", method = RequestMethod.POST)
public String postUser(@ModelAttribute User user) {
// 处理/users/的post请求
// 除了@ModelAttribute绑定之外,还可以通过@RequestParam从页面中传参
users.put(user.getId(), user);
return "success";
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public User getUser(@PathVariable Long id) {
return users.get(id);
}
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public String putUser(@PathVariable Long id, @ModelAttribute User user) {
User u = users.get(id);
u.setName(user.getName());
u.setAge(user.getAge());
users.put(id, u);
return "success";
}
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public String deleteUser(@PathVariable Long id) {
users.remove(id);
return "success";
}
}
controller的测试代码
package com.learn.springbootresuful;
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
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.SpringBootConfiguration;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.learn.springbootresuful.web.HelloController;
import com.learn.springbootresuful.web.UserController;
/**
*
* @author liman
* @createtime 2018年8月22日
* @contract 15528212893
* @comment:
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitWebConfig(classes = MockServletContext.class)
@SpringBootConfiguration
@WebAppConfiguration
public class UserControllerTests {
private MockMvc mvc;
@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.standaloneSetup(new HelloController(), new UserController()).build();
}
/**
* 测试获取用户信息列表,应该为空
*
* @throws Exception
*/
@Test
public void getHello() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andExpect(status().isOk());
}
@Test
public void testUserController() throws Exception {
RequestBuilder request = null;
// 1、get查一下user列表,应该为空
request = get("/users/");
mvc.perform(request).andExpect(status().isOk()).andExpect(content().string(equalTo("[]")));
// 2、post提交一个user
request = post("/users/").param("id", "1").param("name", "测试大师").param("age", "20");
mvc.perform(request)
// .andDo(MockMvcResultHandlers.print())
.andExpect(content().string(equalTo("success")));
// 3、get获取user列表,应该有刚才插入的数据
request = get("/users/");
mvc.perform(request).andExpect(status().isOk())
.andExpect(content().string(equalTo("[{\"id\":1,\"name\":\"测试大师\",\"age\":20}]")));
// 4、put修改id为1的user
request = put("/users/1").param("name", "测试终极大师").param("age", "30");
mvc.perform(request).andExpect(content().string(equalTo("success")));
// 5、get一个id为1的user
request = get("/users/1");
mvc.perform(request).andExpect(content().string(equalTo("{\"id\":1,\"name\":\"测试终极大师\",\"age\":30}")));
// 6、del删除id为1的user
request = delete("/users/1");
mvc.perform(request).andExpect(content().string(equalTo("success")));
// 7、get查一下user列表,应该为空
request = get("/users/");
mvc.perform(request).andExpect(status().isOk()).andExpect(content().string(equalTo("[]")));
}
}
静态资源的访问(Thymeleaf)
spring boot 中默认的静态资源存放在classpath下
spring boot提供了一些模板用于渲染html页面常见的有Thymeleaf、FreeMarker、Velocity、Groovy、Mustache。这里对这些也不做详细介绍
引入Thymeleaf 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Controller代码
@Controller
@RequestMapping(value="/hello")
public class HelloController {
@RequestMapping("/")
public String index(ModelMap map) {
map.addAttribute("host","http://www.baidu.com");
return "index";
}
}
index.html模板页
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
<h1 th:text="${host}">Hello World</h1>
</body>
</html>
测试结果
全局异常的处理
spring boot中提供了一个默认的异常处理请求/error,如果业务代码中抛出异常了,spring boot会去请求这个路径,然后跳转到spring boot默认的异常页面,这个非常不友好。了解全局的异常处理,另一方面对以后养成良好的开发习惯也有一些帮助。
1、定义统一的异常处理类
@ControllerAdvice
public class GlobalExceptionHandler {
public static final String DEFAULT_ERROR_VIEW="error";
//这里注解中需要指定异常类型,根据不同的异常,会有不同的处理函数
@ExceptionHandler(value = Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest request,Exception exception) {
ModelAndView mav = new ModelAndView();
mav.addObject("exception",exception);
mav.addObject("url",request.getRequestURL());
mav.setViewName(DEFAULT_ERROR_VIEW);
return mav;
}
}
@ControllerAdvice定义统一的异常处理类,@ExceptionHandler(value=[异常类型]),上述代码中的DEFAULT_ERROR_VIEW只是错误页面的路径。
2、error.html页面
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title>异常页面</title>
</head>
<body>
<h1>Error Handler</h1>
<div th:text="${url}"></div>
<div th:text="${exception.message}"></div>
</body>
</html>
3、测试结果
针对如何将错误信息以json格式返回,在做实验的时候出现了异常问题,这个后面会补上
To be continued......(未完待续......)
集成Swagger2
To be continued ......
总结
其实写到这里,spring boot中常用的web模块的东西都已经做了一个简单的总结了,但是都只是一些helloworld,并没有实际深入。