JSON请求格式问题

huangapple 未分类评论42阅读模式
英文:

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到第三方时,我观察到以下问题:

  1. 尽管我使用了@JsonPropertyOrder,但元素的顺序与写入的顺序不同。
  2. 在请求字符串中,FirstName和LastName以小写字母firstNamelastName的形式出现。

我该如何解决这个问题?

英文:

I wrote one request DTO using beans.xml, I used @JsonPropertyOrder and @JsonProperty annotation as follow in beans.xml :

&lt;bean ...&gt;
 &lt;annotations&gt;@JsonPropertyOrder({
            &quot;FirstName&quot;,
            &quot;LastName&quot;
        })
        &lt;property name=&quot;FirstName&quot; type=&quot;java.lang.String&quot; &gt;
            &lt;annotations&gt;@JsonProperty(&quot;FirstName&quot;)&lt;/annotations&gt;
        &lt;/property&gt;
        &lt;property name=&quot;LastName&quot; type=&quot;java.lang.String&quot; &gt;
            &lt;annotations&gt;@JsonProperty(&quot;LastName&quot;)&lt;/annotations&gt;
        &lt;/property&gt;
&lt;/bean&gt;

Also I used the ObjectMapper to convert DTO object to Json string.
But when actually json sent to third party I observed:

  1. although I used @JsonPropertyOrder , order of elements is different then what it is written.
  2. 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:

        &lt;property name=&quot;firstName&quot; type=&quot;java.lang.String&quot; &gt;
            &lt;annotations&gt;@JsonProperty(&quot;FirstName&quot;)&lt;/annotations&gt;
        &lt;/property&gt;
        &lt;property name=&quot;lastName&quot; type=&quot;java.lang.String&quot; &gt;
            &lt;annotations&gt;@JsonProperty(&quot;LastName&quot;)&lt;/annotations&gt;
        &lt;/property&gt;

答案2

得分: 0

看起来对我来说,你在第一个注释元素上漏掉了 &lt;/annotations&gt;

英文:

It looks to me like you are missing &lt;/annotations&gt; on the first annotations element

huangapple
  • 本文由 发表于 2020年4月6日 17:49:51
  • 转载请务必保留本文链接:https://java.coder-hub.com/61057041.html
匿名

发表评论

匿名网友

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

确定