springboot处理blog字段
springboot处理blog字段
欢迎关注博主公众号「Java大师」, 专注于分享Java领域干货文章https://www.javaman.cn/
1、数据库表结构
其中content为longblob字段,代表存入的内容
CREATE TABLE `t_post` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`channel_id` int(11) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`status` int(11) NOT NULL,
`summary` varchar(140) COLLATE utf8_bin DEFAULT NULL,
`tags` varchar(64) COLLATE utf8_bin DEFAULT NULL,
`title` varchar(64) COLLATE utf8_bin DEFAULT NULL,
`views` int(11) NOT NULL,
`weight` int(11) NOT NULL,
`description` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`keywords` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`content` longblob,
PRIMARY KEY (`id`),
KEY `IK_CHANNEL_ID` (`channel_id`)
) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
2、创建对应的实体类model
将content内容生命为byte[]类型
private byte[] content;
package com.dsblog.server.model;
import com.baomidou.mybatisplus.annotation.*;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* <p>
*
* </p>
*
* @author java大师
* @since 2022-05-05
*/
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("t_post")
@ApiModel(value="Post对象", description="")
public class Post implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value="id")
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@ApiModelProperty(value="栏目")
@TableField(value = "channel_id")
private Integer channelId;
@ApiModelProperty(value="创建时间")
@TableField(fill = FieldFill.INSERT)
private LocalDateTime created;
@ApiModelProperty(value="状态")
private Integer status;
@ApiModelProperty(value="概要")
private String summary;
@ApiModelProperty(value="标签")
private String tags;
@ApiModelProperty(value="标题")
private String title;
@ApiModelProperty(value="访问次数")
private Integer views;
@ApiModelProperty(value="权重")
private Integer weight;
@ApiModelProperty(value="描述")
private String description;
@ApiModelProperty(value="关键词")
private String keywords;
@ApiModelProperty(value="内容")
@JsonDeserialize(using = PostDeserializer.class)
private byte[] content;
}
3、创建反序列化注释类
package com.dsblog.server.config;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public class PostDeserializer extends JsonDeserializer {
@Override
public Object deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
ObjectMapper mapper = (ObjectMapper) jsonParser.getCodec();
JsonNode textNode = mapper.readTree(jsonParser);
return textNode.asText().toString().getBytes("UTF-8");
}
}
4、修改model类的content,增加注解
@JsonDeserialize(using = PostDeserializer.class)
private byte[] content;
5、添加post信息
@ApiOperation(value = "添加文章")
@PostMapping("/")
public ResultBean addPost(@RequestBody Post post){
if (postService.saveOrUpdate(post)){
return ResultBean.success("添加成功");
}
return ResultBean.error("添加失败");
}
6、测试
1-输入请求参数,点击发送

2-content已经插入成功

注意:如果不对content进行反序列化,添加会报如下错误:
Resolved [org.springframework.http.converter.HttpMessageNotReadableException:
JSON parse error: Invalid UTF-8 start byte 0xa4;
nested exception is com.fasterxml.jackson.databind.JsonMappingException:
Invalid UTF-8 start byte 0xa4<LF> at [Source: (PushbackInputStream); line: 3, column: 20]
(through reference chain: com.xxxx.model.Post["content"])]
springboot处理blog字段的更多相关文章
- springBoot+MybatisPlus数据库字段使用驼峰命名法时报错
假如有个实体类: package com.jeff.entity; public class User { /** * 主键id */ private Integer id; /** * 登陆名 */ ...
- SpringBoot 定义通过字段验证
第一步:定义ValidationResult类 public class ValidationResult { // 校验结果是否有错 private boolean hasErrors = fals ...
- SpringBoot 使用Mybatis-Plus
简介 Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发.提高效率而生. 特性 无侵入:Mybatis-Plus 在 My ...
- springboot教程
http://www.cnblogs.com/java-zhao/tag/spring-boot/ http://blog.csdn.net/liaokailin/article/category/5 ...
- Kotlin开发springboot项目(三)
Kotlin开发springboot项目(三) 在线工具 https://www.sojson.com IDEA中Kotlin生成可执行文件1,项目使用Gradle构建2,在model的build.g ...
- 关于SpringBoot的自动配置和启动过程
一.简介 Spring Boot简化了Spring应用的开发,采用约定大于配置的思想,去繁从简,很方便就能构建一个独立的.产品级别的应用. 1.传统J2EE开发的缺点 开发笨重.配置繁多复杂.开发效率 ...
- Spring Cloud 和dubbo
一.SpringCloud微服务技术简介 Spring Cloud 作为Java 语言的微服务框架,它依赖于Spring Boot,有快速开发.持续交付和容易部署等特点.Spring Cloud 的组 ...
- Mybatis - 动态sql
learn from:http://www.mybatis.org/mybatis-3/dynamic-sql.html mybatis支持动态拼接sql语句.主要有: if choose (when ...
- Django Form and Modelform Admin定义 高级查询)
Django的form表单一般具有两种功能 1. 验证输入 2.输入HTML ---------模板----------- from django import forms class BookFor ...
随机推荐
- jpa CriteriaQueryNo explicit selection and an implicit one could not be determined
java.lang.IllegalArgumentException: Error occurred validating the Criteria ... Caused by: java.lang. ...
- 使用 Spring 通过什么方式访问 Hibernate?
在 Spring 中有两种方式访问 Hibernate:控制反转 Hibernate Template 和 Callback.继承 HibernateDAOSupport 提供一个 AOP 拦截器.
- Java 线程数过多会造成什么异常?
1.线程的生命周期开销非常高 2.消耗过多的 CPU 资源 如果可运行的线程数量多于可用处理器的数量,那么有线程将会被闲置.大量空 闲的线程会占用许多内存,给垃圾回收器带来压力,而且大量的线程在竞争 ...
- spring 提供了哪些配置方式?
基于 xml 配置 bean 所需的依赖项和服务在 XML 格式的配置文件中指定.这些配置文件通常 包含许多 bean 定义和特定于应用程序的配置选项.它们通常以 bean 标签开 头. 例如: &l ...
- SVN之屏蔽不需要提交的xml等文件
SVN之屏蔽不需要提交的xml等文件 在默认"Default changelist"中是我们正常需要提交的文件 在"不需要提交的文件"中存储的是一些线下环境需要 ...
- Linux内核, 编译一个自己的内核
本文,我们将一步一步地介绍如何从源代码编译和安装一个Linux内核.需要注意的是本指导基于Ubuntu 20.04版本编译安装,其它发行版可能会有差异. 在前面文章中我们反复提到过Linux内核, ...
- 搞半天,全国34个省份包含湾湾\香港\澳门的高德poi兴趣点23类数据终于爬完事了
1.技术架构: python+阿里云数据库mongodb5.0+高德地图rest api 2.成本: 阿里云数据库mongodb5.0一个月话费1k多 2.遇到的问题 1)两个阿里云账号下 mongo ...
- 对height 100%和inherit的总结
对height 100%和inherit的总结 欢迎大家来我的博客留言:https://sxq222.github.io/CSS%...博客主页:https://sxq222.github.io 正文 ...
- H5使用Canvas绘图
一.什么是Canvas Canvas 是H5的一部分,允许脚本语言动态渲染图像.Canvas 定义一个区域,可以由html属性定义该区域的宽高,javascript代码可以访问该区域,通过一整套完整的 ...
- vue项目处理dpr和多屏幕适配问题
<!DOCTYPE html> <html style="font-size:37.5px"> <head> <meta charset= ...