springboot配置文件和注解

配置文件

热部署

  • 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插件自带的一个日记注解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值