英文:
JSON request formatting issue
问题
我使用了beans.xml编写了一个请求DTO,并在beans.xml中使用了@JsonPropertyOrder和@JsonProperty注解,如下所示:
<bean ...>
<annotations>@JsonPropertyOrder({
"FirstName",
"LastName"
})
<property name="FirstName" type="java.lang.String" >
<annotations>@JsonProperty("FirstName")</annotations>
</property>
<property name="LastName" type="java.lang.String" >
<annotations>@JsonProperty("LastName")</annotations>
</property>
</bean>
此外,我使用了ObjectMapper将DTO对象转换为JSON字符串。但是当实际发送JSON到第三方时,我观察到以下问题:
- 尽管我使用了@JsonPropertyOrder,但元素的顺序与写入的顺序不同。
- 在请求字符串中,FirstName和LastName以小写字母firstName和lastName的形式出现。
我该如何解决这个问题?
英文:
I wrote one request DTO using beans.xml, I used @JsonPropertyOrder and @JsonProperty annotation as follow in beans.xml :
<bean ...>
<annotations>@JsonPropertyOrder({
"FirstName",
"LastName"
})
<property name="FirstName" type="java.lang.String" >
<annotations>@JsonProperty("FirstName")</annotations>
</property>
<property name="LastName" type="java.lang.String" >
<annotations>@JsonProperty("LastName")</annotations>
</property>
</bean>
Also I used the ObjectMapper to convert DTO object to Json string.
But when actually json sent to third party I observed:
- although I used @JsonPropertyOrder , order of elements is different then what it is written.
- In request string FirstName and LastName is coming as firstName , lastName (small initial letters)
how can I resolve this?
答案1
得分: 0
我猜你使用@JsonProperty的原因是你的JSON参数的名称(FirstName、LastName)与你的Java类(firstName、lastName)不同。你应该将Java参数的名称放在property标签中,这样做:
<property name="firstName" type="java.lang.String">
<annotations>@JsonProperty("FirstName")</annotations>
</property>
<property name="lastName" type="java.lang.String">
<annotations>@JsonProperty("LastName")</annotations>
</property>
英文:
I guess the reason why you're using @JsonProperty is that the name of the parameters in your json (FirstName, LastName) are different from your java class (firstName, lastName). You should put the name of your java parameter in in the property tag, so:
<property name="firstName" type="java.lang.String" >
<annotations>@JsonProperty("FirstName")</annotations>
</property>
<property name="lastName" type="java.lang.String" >
<annotations>@JsonProperty("LastName")</annotations>
</property>
答案2
得分: 0
看起来对我来说,你在第一个注释元素上漏掉了 </annotations>
。
英文:
It looks to me like you are missing </annotations>
on the first annotations element
专注分享java语言的经验与见解,让所有开发者获益!
评论