java大师博客网站

Spring Boot 2.0 教程

  • springboot集成springsecurity 1、整合springsecurity 添加pom.xml <dependency> <groupId>org.springframework.boot</grou...
    • 4月前
  • 4、验证码功能 1、编写工具类生成4位随机数 该工具类主要生成从0-9,a-z,A-Z范围内产生的4位随机数 /** * 产生4位随机字符串 */ public static String getCheckCode() { String base...
    • 4月前
  • 3、打印启动信息 3.1 spring Bean实例化流程 基本流程: 1、Spring容器在进行初始化时,会将xml或者annotation配置的bean的信息封装成一个BeanDefinition对象(每一个bean标签或者@bean注解都封...
    • 4月前
  • 前言 在实际项目研发中,需要针对不同的运行环境,如开发环境、测试环境、生产环境等,每个运行环境的数据库...等配置都不相同,每次发布测试、更新生产都需要手动修改相关系统配置。这种方式特别麻烦,费时费力,而且出错概率大。 Spring Boot为我...
    • 4月前
  • springboot在线人数统计 笔者做了一个网站,需要统计在线人数。 在线有两种: 一、如果是后台系统如果登录算在线,退出的时候或者cookie、token失效的时候就算下线 二、如果是网站前台,访问的时候就算在线 今天我们来讲一下第2种情况,...
    • 4月前
  • 手写springboot starter 一、创建自己的springboot-starter 好的,下面是手写一个自己的Spring Boot Starter自动装配的流程和代码: 1、创建一个Maven项目,命名为my-spring-boot-...
    • 9月前
  • 实际开发中, @RestControllerAdvice和 @ControllerAdvice怎么使用? 在实际开发中,@RestControllerAdvice 和 @ControllerAdvice 的使用场景有所不同。@RestCont...
    • 9月前
  • 2 springsecurity-jwt整合 欢迎关注博主公众号「Java大师」, 专注于分享Java领域干货文章http://www.javaman.cn/sb2/jwt 2.1整合springsecurity 1) <depende...
    • 1年前
  • springboot处理blog字段 欢迎关注博主公众号「Java大师」, 专注于分享Java领域干货文章https://www.javaman.cn/ 1、数据库表结构 其中content为longblob字段,代表存入的内容 CREA...
    • 1年前
  • Springboot+MybatisPlus多数据源比对数据 欢迎关注博主公众号「Java大师」, 专注于分享Java领域干货文章https://www.javaman.cn/ 一、工程目录 二、Mybatisplus多数据源配置 1、po...
    • 1年前
springboot处理blog字段 - java大师  java大师博客网站

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"])]

全部评论: 0

    我有话说: