Lombok的@Value注解会返回小写的JSON。

huangapple go评论67阅读模式
英文:

Lombok @Value returns json in lower case

问题

我在 Spring 和 Lombok 中有一个项目。我有以下的类:

import lombok.Value;

@Value
public class Movement {

    int xAxis;

    int yAxis;

}

这个类在 Spring 响应中被返回。然而,我期待它的返回形式是这样的:

"movement": {
    "xAxis": 1,
    "yAxis": 2
}

但实际返回的却是这样的:

"movement": {
    "xaxis": 1,
    "yaxis": 2
}

字段名变成了小写。我是不是漏掉了什么?

英文:

I have a project in spring and lombok. I have the following class:

import lombok.Value;

@Value
public class Movement {

int xAxis;

int yAxis;

}

This is being returned in a spring response. However I'm expecting it to be returned like this:

"movement": {

 "xAxis":1,
 "yAxis":2
}

but it comes back like this

 "movement": {

  "xaxis":1,
  "yaxis":2
 }

with the fields in lower case. Am I missing something?

答案1

得分: 4

尝试使用 `JsonProperty`

@Value
public class Movement {

@JsonProperty("xAxis")
int xAxis;

@JsonProperty("yAxis")
int yAxis;

}
英文:

Try to use JsonProperty

@Value
public class Movement {

@JsonProperty("xAxis")
int xAxis;

@JsonProperty("yAxis")
int yAxis;

}

答案2

得分: 0

@JsonProperty 定义逻辑属性的名称,即用于属性的 JSON 对象字段名称。如果值为空字符串(默认值),将尝试使用带有注解的字段的名称。

按照以下方式使用此注解:

@JsonProperty("xAxis")
int xAxis;
英文:

@JsonProperty Defines name of the logical property, i.e. JSON object field name to use for the property. If value is empty String (which is the default), will try to use name of the field that is annotated.

Use this annotation as per below :

@JsonProperty("xAxis")
int xAxis;

huangapple
  • 本文由 发表于 2020年8月19日 21:58:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63488644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定