2

I'd like to serialize, via Jackson, my Foo interface:

public interface Foo { }

It has two instances: Standard and Debug responses. I'll only show the Standard response as it adequately conveys my question.

public class FooRegular implements Foo {

     private int number;

     public FooRegular(final int number) { this.number = number; }

     @JsonProperty("NUM")
     public getNumber() { 
       return number;
     }  
}

Note that, I'm returning a Foo in my Controller class:

public Response sendResponse() {
  Foo foo = getFoo(); // returns FooRegular
  return buildResponse(foo); // returns some Response with `foo` as its body
}

However, my response is:

{ number : 1234 }

I looked at this answer, which is what I've tried, but I'm not seeing the expected result.

How can I override the JSON response to have a key of NUM rather than number?

Community
  • 1
  • 1
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
  • I've tried a small test case with your classes and `@JsonProperty` as you have it works fine. What does your `buildResponse` look like? – sherb Nov 14 '14 at 23:17

1 Answers1

0

I tried your FooRegular class with an ObjectMapper and your JsonProperty annotation is working fine. Without knowing what your buildResponse method looks like, I can only assume the problem is in there.

You could try one of these for your resource method:

public Foo sendResponse() {
    return getFoo();
}

or

public Response sendResponse() {
    Foo foo = getFoo();
    ObjectMapper mapper = new ObjectMapper();
    String foostr = mapper.writer().writeValueAsString(foo);
    return Response.ok().entity(foostr).build();
}
sherb
  • 5,845
  • 5
  • 35
  • 45