springboot解析带控制字符的json字符串
在异构系统中,不同的平台数据通信的⽅式多种多样,当前web系统流⾏restful接⼝通信⽅法。
restful接⼝通信的灵魂⽆⾮是json。本⽂适⽤于c#中使⽤newtonsoft ⽣成的json向java开放的restful接⼝通信。
以及SAP向java的restful接⼝进⾏通信。
⽐较接收到的json如下
{validation框架
"json": "{\"HEADER\":{\"AUFNR\":\"100002312156\",\"PLNBEZ\":\"20481438\",\"GAMNG\":\"100.000\",\"GMEIN\":\"PC\",\"GSTRP\":\"20211227\",\"GLTRP\":\"20211229\",\"WERKS\":\"1001\",\"ZKUNNR\":\"\",\"ZNAME1\":\"\",\"STATUS\":\"1\" }
服务端的实体如下
package com.www.dto;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.wwwmonponent.json.SAPOrderReceiveDTODeserializer;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apachemons.lang3.builder.ToStringBuilder;
import javax.validation.Valid;
import straints.NotEmpty;
import straints.NotNull;
import java.util.Set;
@Data
@EqualsAndHashCode(callSuper = false)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@ApiModel("SAP订单接收接⼝⼊参")
public class SAPOrderReceiveDTO {
/***
* 订单头
*/
@Valid
@ApiModelProperty(value = "订单号参见SAPMoHeaderDTO类型")
@NotNull(message = "订单头必填")
@JsonProperty(value = "HEADER")
private SAPMoHeaderDTO header;
/***
* 订单⼯艺
*/
@Valid
@ApiModelProperty(value = "订单⼯序参见SAPMoOperateDTO类型")
@NotEmpty(message = "订单⼯序必填")
@JsonProperty(value = "OPERATES")
private Set<SAPMoOperateDTO> operateList;
/***
* 订单组件
*/
@Valid
@ApiModelProperty(value = "订单组件参见SAPMoComponentDTO类型")
@NotEmpty(message = "订单组件必填")
@JsonProperty(value = "COMPONENTS")
private Set<SAPMoComponentDTO> componentList;
}
笔者⽇常使⽤springboot进⾏web项⽬开发,json解析框架是springboot默认的Jackson,本⽂以Jackson解析带控制字符的json为例。
理论上fastjson是⽀持这种⾮标json的,不过不在本⽂讨论范围内,笔者不太喜欢fastjson,恕不回答fastjson相关的解决⽅案。
实体改造
package com.www.dto;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.wwwmonponent.json.SAPOrderReceiveDTODeserializer;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apachemons.lang3.builder.ToStringBuilder;
import javax.validation.Valid;
import straints.NotEmpty;
import straints.NotNull;
import java.util.Set;
@Data
@EqualsAndHashCode(callSuper = false)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@ApiModel("SAP订单接收接⼝⼊参")
public class SAPOrderReceiveDTO {
@Valid
@JsonDeserialize(using = SAPOrderReceiveDTODeserializer.class)
@JsonProperty(value = "json")
@NotNull(message = "⽆效的json数据")
private JsonNode json;
@Data
public static final class JsonNode
{
/***
* 订单头
*/
@Valid
@ApiModelProperty(value = "订单号参见SAPMoHeaderDTO类型")
@NotNull(message = "订单头必填")
@JsonProperty(value = "HEADER")
private SAPMoHeaderDTO header;
/***
* 订单⼯艺
*/
@Valid
@ApiModelProperty(value = "订单⼯序参见SAPMoOperateDTO类型")
@NotEmpty(message = "订单⼯序必填")
@JsonProperty(value = "OPERATES")
private Set<SAPMoOperateDTO> operateList;
/***
* 订单组件
*/
@Valid
@ApiModelProperty(value = "订单组件参见SAPMoComponentDTO类型")
@NotEmpty(message = "订单组件必填")
@JsonProperty(value = "COMPONENTS")
private Set<SAPMoComponentDTO> componentList;
}
}
新增⾃定义反序列化类
package com.wwwmonponent.json;
import com.JsonParser;
import com.JsonProcessingException;
import com.TreeNode;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.de.ObjectNode;
import com.fasterxml.de.TextNode;
import com.www.dto.SAPOrderReceiveDTO;
import org.springframework.util.StringUtils;
import java.io.IOException;
public class SAPOrderReceiveDTODeserializer extends JsonDeserializer<SAPOrderReceiveDTO.JsonNode> {
JacksonMapperEnhance objectMapper = new JacksonMapperEnhance();
@Override
public SAPOrderReceiveDTO.JsonNode deserialize(final JsonParser jp, final DeserializationContext ctx) throws IOException, JsonProcessingException { final TreeNode node = jp.getCodec().readTree(jp);
String json = null;
if ((node instanceof TextNode)) {
json = ((TextNode) node).textValue();
}
if(node instanceof ObjectNode)
{
json = (((ObjectNode)node).get("json")).textValue();
}
if(StringUtils.isEmpty(json))
return null;
//反序列化:空字符串的对象直接转换成null对象
/
/ 忽略字段⼤⼩写
final SAPOrderReceiveDTO.JsonNode jsonNode = adValue(toHalfWidth(json), SAPOrderReceiveDTO.JsonNode.class);
return jsonNode;
}
/**
* 转半⾓的函数(DBC case)<br/><br/>
* 全⾓空格为12288,半⾓空格为32
* 其他字符半⾓(33-126)与全⾓(65281-65374)的对应关系是:均相差65248
*
* @param input 任意字符串
* @return半⾓字符串
*/
public String toHalfWidth(String input) {
char[] c = CharArray();
for (int i = 0; i < c.length; i++) {
if (c[i] == 12288) {
//全⾓空格为12288,半⾓空格为32
c[i] = (char) 32;
continue;
}
if (c[i] > 65280 && c[i] < 65375)
/
/其他字符半⾓(33-126)与全⾓(65281-65374)的对应关系是:均相差65248
c[i] = (char) (c[i] - 65248);
}
return new String(c);
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。