1. 程式人生 > 其它 >springboot處理blog欄位

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