SpringBoot实战(五)-Exception -服务器端API -Postman(再断更)

2020.3.6

直接原因:UP主有时写的乱,跟不下去

(发觉慕课网的实战课程更加有条理,所以之后记录之前没有写的日志工具,报错提示和Enums)

根本原因:打满鸡血冲了一个星期之后开始怠惰(看似不可避免)

But:其实也不算断了,只不过是学其他的去了,其他的怠惰了这个可能又开始学了:)

(循环鸡血,循环怠惰,说好听点是遵循人性)

ExceptionHandler  //TODO

需要advice.CustomizeExceptionHandler,CommentController,exception包下还有三个class

 

API

写一个服务端的接口(API),使用前后端约定好的Json格式,从前端发送到后端

现在API完成一个插入Comment过程

和以前的Get请求的区别:方法返回类型是Object,加上了@ResponseBody

@ResponseBody的作用其实是将Java对象转为Json格式的数据,会自动反序列化Json为对象

 

网络传输新建CommentDTO

@Data
public class CommentDTO {
    private Long id;
    private Long parentId;
    private Integer type;
    private Long commentator;
    private Long gmtCreate;
    private Long gmtModified;
    private Long likeCount;
    private Integer commentCount;
    private String content;
    private User user;
}

 

@RequestBody CommentDTO commentDTO

新注解@RequestBody可以直接传一个对象,比传参要快

@Controller
public class CommentController {

    @Autowired
    private CommentMapper commentMapper;

    @ResponseBody
    @RequestMapping(value = "/comment", method = RequestMethod.POST)
    public Object post(@RequestBody CommentDTO commentDTO) {
        Comment comment = new Comment();
        comment.setParentId(commentDTO.getParentId());
        comment.setContent(commentDTO.getContent());
        comment.setType(commentDTO.getType());
        comment.setGmtCreate(System.currentTimeMillis());
        comment.setGmtModified(System.currentTimeMillis());
        comment.setCommentator(1);
        commentMapper.insert(comment);
        return null;
    }
}

使用Postman

如果报错content-type有问题:

传入了数据库:

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章