- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我最近将我的应用程序从 Spring Boot 1.5.3 升级到 Spring Boot 2.3.5。
UI 上的一项功能开始损坏,我在调试时发现对 UI 的 json 响应已更改
原回复:
"modelExplanation": [{
"sectionId": 0,
"sectionText": "some text",
"startIndex": "0",
"endIndex": "105",
"modelExplanations": [{
"modelId": "mdl_collusion",
"modelName": "collusion",
"modelVersion": "3.4.1",
"score": "1.0",
"riskStatus": "low"
},
{
"modelId": "mdl_mkt_mnpltn",
"modelName": "market_manipulation",
"modelVersion": "3.4.1",
"score": "1.0",
"riskStatus": "low"
},
{
"modelId": "mdl_secrecy",
"modelName": "secrecy",
"modelVersion": "3.4.1",
"score": "1.0",
"riskStatus": "low"
}]
}]
}
新响应:在属性名称中有下划线 (_)
"section_id": 1,
"section_text": "some text",
"start_index": "738",
"end_index": "1112",
"model_explanations": [{
"model_id": "mdl_collusion",
"model_name": "collusion",
"model_version": "3.4.1",
"section_score": "0.09059832",
"risk_status": "low"
},
{
"model_id": "mdl_mkt_mnpltn",
"model_name": "market_manipulation",
"model_version": "3.4.1",
"section_score": "0.12787165",
"risk_status": "low"
},
{
"model_id": "mdl_secrecy",
"model_name": "secrecy",
"model_version": "3.4.1",
"section_score": "0.19208406",
"risk_status": "low"
}]
}]
}
似乎@JsonProperty 不起作用。我的类(class)如下:
public ModelExplanationResponse getModelExplanation(User user, ModelRequest request) {
ModelExplanationMetadata metadata = null;
try {
String modelExplanationJson = anotherService.getModelExplanation(user, request.getMessageId(), request.getSource(), request.getAvroId(), request.getRundate());
if (modelExplanationJson != null && !"".equals(modelExplanationJson)) {
metadata = mapJSONToMetadata(modelExplanationJson);
}
} catch (Exception e) {
throw new AlertMngtServiceException("Unable to retrieve attachment",e);
}
return getModelExplanationResponse(metadata, request);
}
private ModelExplanationResponse getModelExplanationResponse(ModelExplanationMetadata metadata, ModelRequest request) {
ModelExplanationResponse response = new ModelExplanationResponse();
if (metadata != null && metadata.getModelExplanation() != null) {
List<ModelExplanationSection> modelExplanation = mapJSONToModelExplanation(metadata.getModelExplanation());
if (!CollectionUtils.isEmpty(modelExplanation)) {
modelExplanation.sort(Comparator.comparing(ModelExplanationSection::getSectionId));
response.setModelExplanation(modelExplanation);
String formattedMessageBody = new ModelExplanationBasedMessageModifier().modify(request.getMessageBody(), modelExplanation);
response.setMessageBody(formattedMessageBody);
}
} else {
LOGGER.info("Unable to fetch Model Explanation for {}", request);
}
return response;
}
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
@JsonIgnoreProperties(ignoreUnknown = true)
public class ModelExplanationSection {
private final int sectionId;
private final String sectionText;
private final String startIndex;
private final String endIndex;
private final List<ModelExplanation> modelExplanations;
@JsonCreator
public ModelExplanationSection(
@JsonProperty("section_id") int sectionId,
@JsonProperty("section_text") String sectionText,
@JsonProperty("start_index") String startIndex,
@JsonProperty("end_index") String endIndex,
@JsonProperty("model_explanations") List<ModelExplanation> modelExplanations) {
super();
this.sectionId = sectionId;
this.sectionText = sectionText;
this.startIndex = startIndex;
this.endIndex = endIndex;
this.modelExplanations = modelExplanations;
}
public int getSectionId() {
return sectionId;
}
public String getSectionText() {
return sectionText;
}
public String getStartIndex() {
return startIndex;
}
public String getEndIndex() {
return endIndex;
}
public List<ModelExplanation> getModelExplanations() {
return modelExplanations;
}
}
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Strings;
@JsonIgnoreProperties(ignoreUnknown = true)
public class ModelExplanation {
private final String modelId;
private final String modelName;
private final String modelVersion;
private final String score;
private final String riskStatus;
@JsonCreator
public ModelExplanation(
@JsonProperty("model_id") String modelId,
@JsonProperty("model_name") String modelName,
@JsonProperty("model_version") String modelVersion,
@JsonProperty("section_score") String score,
@JsonProperty("risk_status") String riskStatus) {
super();
this.modelId = modelId;
this.modelName = modelName;
this.modelVersion = modelVersion;
this.score = score;
this.riskStatus = getRiskStatus(riskStatus);
}
private String getRiskStatus(String riskStatus) {
return Strings.isNullOrEmpty(riskStatus)?"low":riskStatus;
}
public String getModelId() {
return modelId;
}
public String getModelName() {
return modelName;
}
public String getModelVersion() {
return modelVersion;
}
public String getScore() {
return score;
}
public String getRiskStatus() {
return riskStatus;
}
}
我试过 @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) 但它不起作用。
最佳答案
我无法使用由 Spring Boot 2.3.5 引入的 Jackson 2.11.3 来复制它
以下测试正确序列化驼峰命名法。
public class DualNamingStrategy {
public static void main(String[] args) throws JsonProcessingException {
final String json = """
{
"model_id": "mdl_secrecy",
"model_name": "secrecy",
"model_version": "3.4.1",
"section_score": "0.19208406",
"risk_status": "low"
}
""";
final ObjectMapper mapper = new ObjectMapper();
final ModelExplanation explanation = mapper.readValue(json, ModelExplanation.class);
final String serialised = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(explanation);
System.out.println(serialised);
}
}
输出:
{
"modelId" : "mdl_secrecy",
"modelName" : "secrecy",
"modelVersion" : "3.4.1",
"score" : "0.19208406",
"riskStatus" : "low"
}
现在,这不是答案,所以我会在这里提供一个答案,尽管这是一种解决方法,因为问题似乎出在您发布的代码之外的其他地方。
@JsonProperty
注释,添加
Jackson Java 8 Parameter Names module (弥补删除的注释)并使用两个具有不同命名策略的 ObjectMapper:
public class DifferentNamingStrategies {
public static void main(String[] args) throws JsonProcessingException {
final String json = "{\n" +
" \"model_id\": \"mdl_secrecy\",\n" +
" \"model_name\": \"secrecy\",\n" +
" \"model_version\": \"3.4.1\",\n" +
" \"section_score\": \"0.19208406\",\n" +
" \"risk_status\": \"low\"\n" +
"}\n";
// Default object mapper, this has camelCase just like the default in Spring
final ObjectMapper camelMapper = new ObjectMapper()
// This allows us to remove the @JsonProperty annotations from the constructor parameters
.registerModule(new ParameterNamesModule());
// Custom object mapper that we use to deserialise JSON with snake_case
final ObjectMapper snakeMapper = new ObjectMapper()
.registerModule(new ParameterNamesModule())
.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
final ModelExplanation explanation = snakeMapper.readValue(json, ModelExplanation.class);
final String serialised = camelMapper.writerWithDefaultPrettyPrinter().writeValueAsString(explanation);
System.out.println(serialised);
}
}
关于java - springboot升级后@JsonProperty不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67320949/
我有一个 pojo,大多数时候我不想将字段写入 Json,但对于特定的条件,我想将这个值写入 json。例如我的模型是; public class Person { @JsonProperty
虽然类 DataType 的序列化会忽略 dbOptions,但会打印 dataType 及其值。 请注意,我只需要在序列化期间而不是反序列化期间忽略这些属性。 @JsonTypeInfo(use =
这个问题已经有答案了: Jackson enum Serializing and DeSerializer (19 个回答) 已关闭 4 年前。 我有一个简单的枚举,它具有 OK 和 FAULT 属性
我想要生成以下 JSON 输出。 **JSON Output:** { "ingredients": { "main": ["flour", "water", "egg"], "o
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我有以下 POJO(在构造函数等内): public class GlobalData implements Serializable { //constructors getters and s
我有一个要发送的 JSON 示例: {"lat":41.562592,"long":-8.430022}' 我需要引用第二个属性,如下所示: @JsonProperty('long') private
在 Google App Engine NDB 中,有一个属性类型 JsonProperty,它采用 Python list 或字典并自动序列化它。 我的模型的结构取决于这个问题的答案,所以我想知道对
是否有详细说明 JsonProperty 命名约定的文档?就像我声明 @JsonProperty("id") 什么可以和不能放在括号内一样? 最佳答案 任何东西。 JSON 生成器本身会转义奇怪或错误
简而言之,我正在尝试为我编写的休息服务编写一个带有休息保证的集成测试。我的问题是,当服务器尝试反序列化 JSON 时,我收到错误,字段“元数据”未标记为可忽略。我必须处理这个字段,这也意味着(据我所知
是否可以动态设置或在调用时设置所需的@JsonProperty? 这背后的原因...我正在生成描述架构并定义的 json 文件 新项目的必填字段是什么 项目更新需要哪些字段。 因此,创建只需要 foo
@JsonProperty 名称中允许有空格吗?即 @JsonProperty("Jon Snow") private String jonSnow = "Lord"; // getter & set
@Data public class Tests { @JsonProperty("comment") private String notes; } 我知道如何使用@JsonProp
我正在开发一个使用 Jackson 注释的 Spring boot 应用程序。 我想从配置中读取 @JsonProperty 的值,而不是使用常量字符串。 JSON 输入示例 {"s":12} 代码
以下是从服务收到的 json 响应的摘录。 aggregations: { gender: { ...other data } } 服务每次都会返回相同的结构,但
当我使用 Spring RestTemplate 使用一个安静的网络服务时,我得到以下异常 com.fasterxml.jackson.databind.exc.UnrecognizedPropert
我正在尝试创建一个包含来自 Facebook 和 Twitter 的信息的数据类。 但是在我来自 Twitter 的 JSON 回复中我需要 id_str 并且从 FaceBook 我得到 id。 我
如何使用@JsonProperty() 在另一个 json 对象中获取一个 json 对象?我要获取的示例json是: "location" : { "needs_recoding" : fals
jackson 的 @JsonProperty 是否有类级注释?在不知道类中有哪些字段名称的情况下,我可以用注释对类进行注释,它会自动为我映射吗? 目前我必须用 JsonProperty 注释每个字段
我有两个返回此 Json 的端点: "type": { "nbrCurrentRemainingDays": 0, "desc": "put desc here !",
我是一名优秀的程序员,十分优秀!