开发者

JAXB Marshalling & Unmarshalling

开发者 https://www.devze.com 2023-04-06 09:27 出处:网络
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 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

  1. Pass the data as a long comma separated string and at server side split it.
  2. 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
0

精彩评论

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

关注公众号