springboot处理blog字段

欢迎关注博主公众号「Java大师」, 专注于分享Java领域干货文章https://www.javaman.cn/

1、数据库表结构

其中content为longblob字段,代表存入的内容

  1. CREATE TABLE `t_post` (
  2. `id` bigint(20) NOT NULL AUTO_INCREMENT,
  3. `channel_id` int(11) DEFAULT NULL,
  4. `created` datetime DEFAULT NULL,
  5. `status` int(11) NOT NULL,
  6. `summary` varchar(140) COLLATE utf8_bin DEFAULT NULL,
  7. `tags` varchar(64) COLLATE utf8_bin DEFAULT NULL,
  8. `title` varchar(64) COLLATE utf8_bin DEFAULT NULL,
  9. `views` int(11) NOT NULL,
  10. `weight` int(11) NOT NULL,
  11. `description` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  12. `keywords` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  13. `content` longblob,
  14. PRIMARY KEY (`id`),
  15. KEY `IK_CHANNEL_ID` (`channel_id`)
  16. ) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8 COLLATE=utf8_bin

2、创建对应的实体类model

将content内容生命为byte[]类型

private byte[] content;

  1. package com.dsblog.server.model;
  2. import com.baomidou.mybatisplus.annotation.*;
  3. import io.swagger.annotations.ApiModel;
  4. import io.swagger.annotations.ApiModelProperty;
  5. import lombok.Data;
  6. import lombok.EqualsAndHashCode;
  7. import java.io.Serializable;
  8. import java.time.LocalDateTime;
  9. /**
  10. * <p>
  11. *
  12. * </p>
  13. *
  14. * @author java大师
  15. * @since 2022-05-05
  16. */
  17. @Data
  18. @EqualsAndHashCode(callSuper = false)
  19. @TableName("t_post")
  20. @ApiModel(value="Post对象", description="")
  21. public class Post implements Serializable {
  22. private static final long serialVersionUID = 1L;
  23. @ApiModelProperty(value="id")
  24. @TableId(value = "id", type = IdType.AUTO)
  25. private Long id;
  26. @ApiModelProperty(value="栏目")
  27. @TableField(value = "channel_id")
  28. private Integer channelId;
  29. @ApiModelProperty(value="创建时间")
  30. @TableField(fill = FieldFill.INSERT)
  31. private LocalDateTime created;
  32. @ApiModelProperty(value="状态")
  33. private Integer status;
  34. @ApiModelProperty(value="概要")
  35. private String summary;
  36. @ApiModelProperty(value="标签")
  37. private String tags;
  38. @ApiModelProperty(value="标题")
  39. private String title;
  40. @ApiModelProperty(value="访问次数")
  41. private Integer views;
  42. @ApiModelProperty(value="权重")
  43. private Integer weight;
  44. @ApiModelProperty(value="描述")
  45. private String description;
  46. @ApiModelProperty(value="关键词")
  47. private String keywords;
  48. @ApiModelProperty(value="内容")
  49. @JsonDeserialize(using = PostDeserializer.class)
  50. private byte[] content;
  51. }

3、创建反序列化注释类

  1. package com.dsblog.server.config;
  2. import com.fasterxml.jackson.core.JsonParser;
  3. import com.fasterxml.jackson.core.JsonProcessingException;
  4. import com.fasterxml.jackson.databind.DeserializationContext;
  5. import com.fasterxml.jackson.databind.JsonDeserializer;
  6. import com.fasterxml.jackson.databind.JsonNode;
  7. import com.fasterxml.jackson.databind.ObjectMapper;
  8. import java.io.IOException;
  9. public class PostDeserializer extends JsonDeserializer {
  10. @Override
  11. public Object deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
  12. ObjectMapper mapper = (ObjectMapper) jsonParser.getCodec();
  13. JsonNode textNode = mapper.readTree(jsonParser);
  14. return textNode.asText().toString().getBytes("UTF-8");
  15. }
  16. }

4、修改model类的content,增加注解

@JsonDeserialize(using = PostDeserializer.class)

private byte[] content;

5、添加post信息

  1. @ApiOperation(value = "添加文章")
  2. @PostMapping("/")
  3. public ResultBean addPost(@RequestBody Post post){
  4. if (postService.saveOrUpdate(post)){
  5. return ResultBean.success("添加成功");
  6. }
  7. return ResultBean.error("添加失败");
  8. }

6、测试

1-输入请求参数,点击发送

2-content已经插入成功

注意:如果不对content进行反序列化,添加会报如下错误:

  1. Resolved [org.springframework.http.converter.HttpMessageNotReadableException:
  2. JSON parse error: Invalid UTF-8 start byte 0xa4;
  3. nested exception is com.fasterxml.jackson.databind.JsonMappingException:
  4. Invalid UTF-8 start byte 0xa4<LF> at [Source: (PushbackInputStream); line: 3, column: 20]
  5. (through reference chain: com.xxxx.model.Post["content"])]

springboot处理blog字段的更多相关文章

  1. springBoot+MybatisPlus数据库字段使用驼峰命名法时报错

    假如有个实体类: package com.jeff.entity; public class User { /** * 主键id */ private Integer id; /** * 登陆名 */ ...

  2. SpringBoot 定义通过字段验证

    第一步:定义ValidationResult类 public class ValidationResult { // 校验结果是否有错 private boolean hasErrors = fals ...

  3. SpringBoot 使用Mybatis-Plus

    简介 Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发.提高效率而生. 特性 无侵入:Mybatis-Plus 在 My ...

  4. springboot教程

    http://www.cnblogs.com/java-zhao/tag/spring-boot/ http://blog.csdn.net/liaokailin/article/category/5 ...

  5. Kotlin开发springboot项目(三)

    Kotlin开发springboot项目(三) 在线工具 https://www.sojson.com IDEA中Kotlin生成可执行文件1,项目使用Gradle构建2,在model的build.g ...

  6. 关于SpringBoot的自动配置和启动过程

    一.简介 Spring Boot简化了Spring应用的开发,采用约定大于配置的思想,去繁从简,很方便就能构建一个独立的.产品级别的应用. 1.传统J2EE开发的缺点 开发笨重.配置繁多复杂.开发效率 ...

  7. Spring Cloud 和dubbo

    一.SpringCloud微服务技术简介 Spring Cloud 作为Java 语言的微服务框架,它依赖于Spring Boot,有快速开发.持续交付和容易部署等特点.Spring Cloud 的组 ...

  8. Mybatis - 动态sql

    learn from:http://www.mybatis.org/mybatis-3/dynamic-sql.html mybatis支持动态拼接sql语句.主要有: if choose (when ...

  9. Django Form and Modelform Admin定义 高级查询)

    Django的form表单一般具有两种功能 1. 验证输入 2.输入HTML ---------模板----------- from django import forms class BookFor ...

随机推荐

  1. JVM-learning

    JVM是什么?? Java Virtual Mechine JRE(JavaRuntimeEnvironment,Java运行环境),也就是Java平台.所有的Java 程序都要在JRE下才能运行. ...

  2. elasticsearch 是如何实现 master 选举的 ?

    面试官:想了解 ES 集群的底层原理,不再只关注业务层面了. 前置前提: 1.只有候选主节点(master:true)的节点才能成为主节点. 2.最小主节点数(min_master_nodes)的目的 ...

  3. 名词解析-SOA

    什么是SOA SOA全英文是Service-Oriented Architecture,中文意思是中文面向服务编程,是一种思想,一种方法论,一种分布式的服务架构 SOA的作用场景 SOA解决多服务凌乱 ...

  4. 创建Maven web工程

    ---恢复内容开始--- 第一步,启动Eclipse,依次打开菜单[File][New][Other] 找到目录Maven,选择Maven Project, 选择一个Archetype.这里创建Web ...

  5. vue中ajax请求发送

    示例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8& ...

  6. js身份证精确认证

    function checkIdCard(idcard) { // 1 "验证通过!", 0 //身份证号码校验错误 var Errors = new Array( "1 ...

  7. scanf()函数的原理

    最近使用scanf发现了自己对scanf函数还是不太了解,主要出现在无意中出现的一个错误: scanf正确的写法是,scanf中以什么格式输入变量,则变量的类型就应该是什么格式,如下面scanf输入到 ...

  8. 微信小程序调研

    小程序入口 微信发现,小程序 公众号主体查看小程序 好友分享,群分享 公众号自定义菜单跳转 APP页面跳转 第三方服务 附近的小程序 扫普通链接二维码打开小程序 需要后台开启功能,开启后,用户在微信& ...

  9. 深入HTTP协议

    一.HTTP定义 超文本传输协议(HTTP)是一种通信协议,它允许将超文本标记语言(HTML)文档从Web服务器传送到客户端的浏览器. HTTP是一个属于应用层的面向对象协议,由于其简捷.快速的方式, ...

  10. Python 图_系列之基于邻接炬阵实现广度、深度优先路径搜索算法

    图是一种抽象数据结构,本质和树结构是一样的. 图与树相比较,图具有封闭性,可以把树结构看成是图结构的前生.在树结构中,如果把兄弟节点之间或子节点之间横向连接,便构建成一个图. 树适合描述从上向下的一对 ...