配置文件
热部署
- spring.devtools.restart.enabled=true开启
- spring.devtools.restart.additional-paths=src/main/java 扫描更新的类
- 排除静态资源不扫描 .exclude
- springboot2.x支持put,delete方法
- spring.mvc.hiddenmethod.filter.enabled=true
- method=“post”默认是关闭的
注解
@Transactional
在方法前面加上这个注解,即意味着事务提交,sql要么一起成功要么一起失败。同时执行几句sql语句都应该考虑这个问题。
@ControllerAdvice和@ExceptionHandler
@ControllerAdvice 注解定义全局异常处理类
@ExceptionHandler 注解声明异常处理方法
@ControllerAdvice
public class CustomizeExceptionHandler {
@ExceptionHandler(Exception.class)
ModelAndView handle(Throwable e, Model model, HttpServletRequest request, HttpServletResponse response) {
String contentType = request.getContentType();
//返回json
if ("application/json".equals(contentType)) {
ResultDTO resultDTO;
if (e instanceof CustomizeException) {
resultDTO = ResultDTO.errorOf((CustomizeException) e);
} else {
resultDTO = ResultDTO.errorOf(CustomizeErrorCode.SYS_ERROR);
}
try {
response.setContentType("application/json");
response.setStatus(200);
response.setCharacterEncoding("UTF-8");
PrintWriter writer = response.getWriter();
writer.write(JSON.toJSONString(resultDTO));
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
} else {
//返回错误页面
if (e instanceof CustomizeException) {
model.addAttribute("message", e.getMessage());
} else {
model.addAttribute("message", CustomizeErrorCode.SYS_ERROR.getMessage());
}
return new ModelAndView("error");
}
}
}
@Value注释
该注解会在配置文件中找到key为github.cilent.id相应的value值,注入cilentId
//例子
@Value("${github.cilent.id}")
private String cilentId.properties
//配置文件中有
github.cilent.id=ffofaogjagj
@ResponseBody
这个注解可以将返回的对象转化为json数据类型
@ResponseBody
@PostMapping("/comment")
public ResultDTO post(@RequestBody CommentCreateDTO commentCreateDTO,
HttpServletRequest request){}
@RequestBody
这个注解可以将前端传来的数据绑定到上面的commentCreateDTO对象中
$.ajax({
type: "POST",
url: "/comment",
contentType: 'application/json',
data: JSON.stringify({
"parentId": targetId,
"content": content,
"type": type
}),
@RequestParam
可以获取到127.0.0.1:8080/callback?code=xxx&&state=1 相应的code的值和state的值给到后面String的code和state。还有其他参数:
@RequestParam(name = “state”,required = false,defaultValue = “wd”)
@GetMapping("/callback")
public String callback(@RequestParam(name = "code") String code,
@RequestParam(name = "state") String state,
HttpServletRequest request,
HttpServletResponse response)
@PathVariable
可以取到url的id这个值
@GetMapping("/notification/{id}")
public String profile(HttpServletRequest request,
@PathVariable(name = "id") Long id)
其他的注解
@Slf4j
lombok插件自带的一个日记注解