Java方法重载与REST – org.codehaus.jackson.map.JsonMappingException

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

Java method Overloading with REST - org.codehaus.jackson.map.JsonMappingException

问题

我有一个REST服务,其中有一个POST端点。这个POST端点需要在其请求体中接收一个对象(TravelRequisitionFormDTO):

@POST
@Path("/request")
@ApiOperation(value="Request a trip. Submit the trip request.")
@ApiResponses({
        @ApiResponse(code=200, message="Success"),
        @ApiResponse(code=404, message="Not Found")
})
@Produces({ MediaType.APPLICATION_JSON })
public Response getSubmitTrip(@HeaderParam("Authorization") String token, @ApiParam(required = true) TravelRequisitionFormDTO travelRequisitionFormDTO, @Context HttpServletRequest request)  {
        ...
}

所以当我调用这个端点时,我得到了以下错误:

<p><b>message</b> <u>org.codehaus.jackson.map.JsonMappingException: Conflicting setter definitions for property
        "contactMethods": utility.dataobjects.ContactObject#setContactMethods(1 params) vs
        utility.dataobjects.ContactObject#setContactMethods(1 params)</u></p>
<p><b>description</b> <u>The request sent by the client was syntactically incorrect
        (org.codehaus.jackson.map.JsonMappingException: Conflicting setter definitions for property
        "contactMethods": utility.dataobjects.ContactObject#setContactMethods(1 params) vs
        utility.dataobjects.ContactObject#setContactMethods(1 params)).</u></p>

错误的原因是因为TravelRequisitionFormDTO有一个成员变量(称为ContactObject),该变量有两个重载的方法。因此,在尝试将JSON请求体转换为Java对象时,它不知道该使用哪个重载的方法。我猜它将其视为模糊不清。

public void setContactMethods(ArrayList list)


public void setContactMethods(String[] list)

如果可能的话,我不想更改ContactObject,因为它在许多其他地方都被使用。

问题

有没有办法可以解决这个问题?也就是说,JSON请求体能够成功转换为Java对象吗?

英文:

I have a REST service that has a POST endpoint. This POST endpoint needs to receive an object (TravelRequisitionFormDTO) as part of its body:

@POST
@Path(&quot;/request&quot;)
@ApiOperation(value=&quot;Request a trip. Submit the trip request.&quot;)
@ApiResponses({
        @ApiResponse(code=200, message=&quot;Success&quot;),
        @ApiResponse(code=404, message=&quot;Not Found&quot;)
})
@Produces({ MediaType.APPLICATION_JSON })
public Response getSubmitTrip(@HeaderParam(&quot;Authorization&quot;) String token, @ApiParam(required = true) TravelRequisitionFormDTO travelRequisitionFormDTO, @Context HttpServletRequest request)  {
            ...
    }

So when I call the endpoint, I get the following error:

&lt;p&gt;&lt;b&gt;message&lt;/b&gt; &lt;u&gt;org.codehaus.jackson.map.JsonMappingException: Conflicting setter definitions for property
		&amp;quot;contactMethods&amp;quot;: utility.dataobjects.ContactObject#setContactMethods(1 params) vs
		utility.dataobjects.ContactObject#setContactMethods(1 params)&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;description&lt;/b&gt; &lt;u&gt;The request sent by the client was syntactically incorrect
		(org.codehaus.jackson.map.JsonMappingException: Conflicting setter definitions for property
		&amp;quot;contactMethods&amp;quot;: utility.dataobjects.ContactObject#setContactMethods(1 params) vs
		utility.dataobjects.ContactObject#setContactMethods(1 params)).&lt;/u&gt;&lt;/p&gt;

The reason for the error is because the TravelRequisitionFormDTO has a member variable (called ContactObject) that has two methods that are overloaded. So when it tries to convert the JSON body to JAVA, I guess it does not know which overloaded method to use. I think it sees it as ambiguous.

public void setContactMethods(ArrayList list)

and

public void setContactMethods(String[] list)

I don't want to change ContactObject if possible, because it is used in a number of other places.

Question

Is there any way I can resolve this? i.e. so that the JSON body can be converted successfuly into the Java object?

答案1

得分: 1

你可以保留接受列表的单个属性。而且你的Contract对象可以同时使用数组和列表。

英文:

you can keep single property accepting List. and your Contractobject can consume both Array & List.

答案2

得分: 0

你可以使用Jackson的@JsonSetter注解为一个setter方法添加注解:

@JsonSetter
public void setContactMethods(ArrayList list)

确保你使用了正确的包。在你的情况下,应该使用org.codehaus.jackson.annotate.JsonSetter,就像你在错误消息中看到的那样。可能会出现你的类路径中也有com.fasterxml.jackson.annotation.JsonSetter,所以你要小心不要混淆。

或者,你也可以使用@JsonProperty注解作为替代。

英文:

You could annotate one setter with Jackson @JsonSetter annotation:

@JsonSetter
public void setContactMethods(ArrayList list)

Make sure that you use right package. In your case it would be org.codehaus.jackson.annotate.JsonSetter like you can see in the error message. It might happen that you have also com.fasterxml.jackson.annotation.JsonSetter in the classpath so you have to be careful not to mix it.

Alternatively you can use @JsonProperty instead.

huangapple
  • 本文由 发表于 2020年10月1日 17:05:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64152205.html
匿名

发表评论

匿名网友

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

确定