宁可枝头抱香死,何曾吹落北风中。这篇文章主要讲述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="https://www.songbingjia.com/android/Post对象", description="")
public class Post implements Serializable
private static final long serialVersionUID = 1L;
@ApiModelProperty(value="https://www.songbingjia.com/android/id")
@TableId(value = "https://www.songbingjia.com/android/id", type = IdType.AUTO)
private Long id;
@ApiModelProperty(value="https://www.songbingjia.com/android/栏目")
@TableField(value = "https://www.songbingjia.com/android/channel_id")
private Integer channelId;
@ApiModelProperty(value="https://www.songbingjia.com/android/创建时间")
@TableField(fill = FieldFill.INSERT)
private LocalDateTime created;
@ApiModelProperty(value="https://www.songbingjia.com/android/状态")
private Integer status;
@ApiModelProperty(value="https://www.songbingjia.com/android/概要")
private String summary;
@ApiModelProperty(value="https://www.songbingjia.com/android/标签")
private String tags;
@ApiModelProperty(value="https://www.songbingjia.com/android/标题")
private String title;
@ApiModelProperty(value="https://www.songbingjia.com/android/访问次数")
private Integer views;
@ApiModelProperty(value="https://www.songbingjia.com/android/权重")
private Integer weight;
@ApiModelProperty(value="https://www.songbingjia.com/android/描述")
private String description;
@ApiModelProperty(value="https://www.songbingjia.com/android/关键词")
private String keywords;
@ApiModelProperty(value="https://www.songbingjia.com/android/内容")
@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,增加注解
【springboot处理blog字段】@JsonDeserialize(using = PostDeserializer.class)5、添加post信息
private byte[] content;
@ApiOperation(value = "https://www.songbingjia.com/android/添加文章")
@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[推荐阅读
- 通俗理解大数据及其应用价值
- Intellij官方中文语言包,它来了
- [OpenCV实战]34 使用OpenCV进行图像修复
- 一次实战挖掘软件逻辑漏洞
- vim的简单使用
- 跨越时空的对白——async&await分析
- C语言_Linux基本命令与C语言基础
- NAT实验
- #私藏项目实操分享#愚公系列2022年05月 Python教学课程 75-DRF框架之排序