开发者

Exposing a subset of bean properties as XML response of REST service

开发者 https://www.devze.com 2022-12-21 17:06 出处:网络
I\'m not sure how to best explain this, so this may be a bit long, please bear with me. Let\'s say I have a java bean with a bunch of properties:

I'm not sure how to best explain this, so this may be a bit long, please bear with me.

Let's say I have a java bean with a bunch of properties:

public interface Customer {
    public String getFirstName();
    public String getLastName();
    public Address getAddress(); // embedded bean with its own properties
    ... // lots more
}

There's a corresponding implementation, obviously, plus appropriate setters are defined as well (though setters may not be exposed in the interface for some properties).

There's a service (POJO interface + implementation) that defines a bunch of operations that can be performed on customers (CRUD, some find() queries, etc...). Service is bootstrapped via Spring and the whole thing works fine locally and via RMI.

Now I have a client (iPhone app) which needs access to a small subset of functionality exposed via above API - subset both in terms of service operations and API properties. The idea is to expose this subset via REST-style API:

GET /services/customers/1234

returns

<customer>
  <firstName>Homer</firstName>
  <lastName>Simpson</lastName>
  <city>Springfield</city>
</customer>

or maybe even

<customer firstName="Homer" lastName="Simpson" city="Springfield" />

The question is - how do I go about doing this? I've (briefly) looked at JAXB / CXF / Jersey and it looks like I'd have to define a schema, generate a bunch of classes based on it and copy data from my API entities to those classes. Is there a way to avoid all that?

Ideally I'd like to ann开发者_如何学Gootate the appropriate properties in my API entities and have them (and only them) marshalled "automagically". I'm ok with writing dedicated interface / implementation for the web service endpoint if needed; annotating the existing one would be even better.

The other (more pressing) question is - am I doing something stupid? Is there a simpler and / or better way? Last time I've worked with web services was Axis 1.1 times and last time I've worked with iPhone was never :-) so any pointers will be much appreciated.

Update: To clarify - I'm looking for the simplest way to marshall a subset of javabean properties to XML (or JSON maybe?) without having to write / generate an additional class to represent said subset of properties. The above example is an oversimplification, in reality the API is quite extensive with many classes involved. I really want to avoid duplication if possible.


Googling jaxb interface seems to indicate interfaces are a pain to work with in JAXB, as seen in this java.net blog post and another stackoverflow question.

It may be easier if your POJO is a class rather than an interface. I'm using RESTEasy (another JAX-RS implementation) with JAXB and I only need to annotate my POJO class with @XmlRootElement. On the other side, I've written custom http client code to marshall/unmarshall the same annotated class with JAXB (would be automagic with RESTEasy client proxy framework if I can use that). Note that I don't have a schema defined, but I can get away with a lot since I'm in control of both sides of the RESTful web service.

Jersey has a client API you might want to take a look at. CXF also has a front-end / client API including a proxy-based mechanism similar to RESTEasy.

Take a look at the RESTful Java with JAX-RS book which covers the following that I've found helpful:

  • REST service concepts
  • overview of the various implementations including client API support
  • code examples

Example controller:

@GET
@Produces( { MediaType.APPLICATION_XML })
@Path("/customers/{id}")
public Customer getCustomer(@PathParam("id") int id) {
    ...
    return customer;
}

Example POJOs:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
  Address address;
  String name;

  // annotate with XmlTransient to prevent mapping this property/type to XML
  @XmlTransient
  String ssn;
  ...
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Address {
  String planet;
  ...
}

XmlTransient Java doc


My preference would be to annotate the classes you already have with JAXB annotations and have the binding compiler create the schema based off of the classes you already have by running schemagen against the annotated source files example here.

I've found JAXB to be extremely useful and relativel easy to use in creating schemas from java classes and java classes from schemas that I use in creating web services used for servicing insurance policies here at work.


Easiest way to go about it: Use a XML serialization API such as XStream or Simple, which are made exactely for this kind of POJO to XML kind of question you are asking. Selecting sub-sets and switching to attributes is easy as the following:

class Customer {
    @XStreamAsAttribute
    String firstName;
    @XStreamAsAttribute
    String lastName;
    @XStreamOmitField
    Address address();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消