JAXB Marshalling & Unmarshalling
I am trying to study how the REST service approach works using Jersey.
开发者_JAVA技巧I have come up with 2 options when creating and accessing a REST service. I have 6 parameters which are all string
- Pass the data as a long comma separated string and at server side split it.
- Use JAXB and do Marshalling and Unmarshalling.
I can understand that 1st option will be the fastest but does anyone knows how much fast it will be than the 2nd option and is it a safe and efficient way to do this.
- It will be nice if someone can mention any more options that are possible..
Thanks
You'll have to write your own MessageBodyReader/Writter if you want the comma separated string. Also you'll need to make sure the parameter itself does not contain a comma, etc. Not that it would be a blocker - just noting that.
You can also use low-level JSON marshaling/unmarshaling using Jettison - that should also be pretty fast. Or use jackson. See various JSON mapping options in Jersey user-guide.
Just for completeness, another option might be to use Form (which is essentially a map of String->List) - if you use that, no need for a special MessageBodyReader/Writter - Jersey will handle it for you. You just need to annotate your methods with @Produce/@Consume("application/www-form-urlencoded").
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.
Using a JAXB implementation with Jersey will give you the option of passing an XML or JSON message that will be easy for many clients to interact with. Inventing your own format for the sake of an unknown performance gain is most likely an unnecessary micro optimization.
Here is an example I put together using Jersey & MOXy in GlassFish:
- Part 1 - The Database
- Part 2 - Mapping the Database to JPA Entities
- Part 3 - Mapping JPA entities to XML (using JAXB)
- Part 4 - The RESTful Service
- Part 5 - The Client
精彩评论