1

I'd like to expose an interface through a web service. Instead of the "interface view" the actual implementation is exposed.

Example:

@XmlRootElement
public interface Foo extends Serializable {    
    String getId();
}

@XmlRootElement
public class MyFoo implements Foo {
    private String id;
    private String bar;
    // Getter / Setter
}


@GET
@Path("myfoo")
@Produces(MediaType.APPLICATION_XML)
public MyFoo myFoo() {
    MyFoo foo = new MyFoo();
    foo.setId("myfoo");
    foo.setBar("bar");
    return foo;
}

This web service method will give the expected result like

<myFoo>
  <bar>bar</bar>
  <id>myfoo</id>
</myFoo>

The problem occurs when I try to expose the interface view only:

@GET
@Path("foo")
@Produces(MediaType.APPLICATION_XML)
public Foo foo() {
    MyFoo myFoo = new MyFoo();
    myFoo.setId("foo");
    myFoo.setBar("bar");
    Foo foo = myFoo;
    return foo;
}

This will give the result:

<myFoo>
  <bar>bar</bar>
  <id>foo</id>
</myFoo>

But what I expect is a response like this:

<foo>
  <id>foo</id>
</foo>

How am I able to achieve this?

stg
  • 2,757
  • 2
  • 28
  • 55
  • I don't actually think there is an API way yet to do this; Specifically the Jackson processor has a `JsonView` annotation that allows you to do it. Example: http://stackoverflow.com/questions/26775106/jsonview-of-jackson-not-working-with-jax-rs – Gimby Aug 08 '16 at 08:32

0 Answers0