users@jersey.java.net

[Jersey] Re: ObjectMapper.writeValueAsString causes java.lang.IllegalStateException

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Tue, 24 Mar 2015 19:24:55 +0100

Hi Pritam,

That was quick. Thanks for sharing the solution with others!

IIUC, your bean now looks like bellow. This seems to me just like a Jackson backwards compatibility issue.

~Jakub

@JsonInclude(Include.NON_NULL)
@XmlRootElement(name = "MyBean")
@MailBean
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyBean {

@JsonProperty(“my_elements”)
private List<MyElement> myElements;

@XmlElement
public List<MyElement> getMyElements() {
    return myElements;
}

@JsonProperty(“my_elements")
public void setMyElements(final List<MyElement> elements) {
    this.myElements = elements;
}
}


> On 24 Mar 2015, at 19:17, Pritam Moodbidri <pritam_at_yahoo-inc.com> wrote:
>
> Hi,
>
> So I added the @JsonProperty to the getter and the declaration of the element, and it works now. It used to work with the previous version of Jersey and Jackson, without those additional places being annotated.
>
> - Pritam
>
>
>
> On Tuesday, March 24, 2015 11:01 AM, Pritam Moodbidri <pritam_at_yahoo-inc.com> wrote:
>
>
> Hi Jakub
>
> The bean looks like this
>
> @JsonInclude(Include.NON_NULL)
> @XmlRootElement(name = "MyBean")
> @MailBean
> @JsonIgnoreProperties(ignoreUnknown = true)
> public class MyBean {
> private List<MyElement> myElements;
>
> @XmlElement
> public List<MyElement> getMyElements() {
> return myElements;
> }
>
> @JsonProperty(“my_elements")
> public void setMyElements(final List<MyElement> elements) {
> this.myElements = elements;
> }
> }
>
> And, I get the exception when I have a
>
> final String requestJson = mapper.writeValueAsString(request);
>
>
> (request is a MyBean object)
>
> Thanks
>
> - Pritam
>
>
>
> On Tuesday, March 24, 2015 10:33 AM, Jakub Podlesak <jakub.podlesak_at_oracle.com> wrote:
>
>
> Hi Pritam,
>
> Not sure. Would you mind sharing a bit more information on your bean and your method?
> Source code snippet will work best.
>
> Have you tried to ask at the Jacskon forum, http://jackson-users.ning.com/forum <http://jackson-users.ning.com/forum>?
>
> Cheers,
>
> ~Jakub
>
>
>> On 24 Mar 2015, at 18:17, Pritam Moodbidri <pritam_at_yahoo-inc.com <mailto:pritam_at_yahoo-inc.com>> wrote:
>>
>> Hi,
>>
>> I am upgrading Jersey from 2.7 to 2.17, and also relatively Jackson from 1.x to 2.x.
>>
>> One of the function was using ObjectMapper.writeValueAsString, and using the bean as an argument. The bean is using @JsonProperty for an element in it, which has a different name, than the element variable name, which is throwing the exception
>>
>> java.lang.IllegalStateException: Conflicting/ambiguous property name definitions (implicit name 'myElements'): found multiple explicit names: [myElements, my_elements], but also implicit accessor: [field .....#myElements][visible=false,ignore=false,explicitName=false]
>>
>> Do I need to add something more with Jersey 2.17?
>>
>> - Pritam
>
>
>
>
>