You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by hanasaki jiji <ha...@gmail.com> on 2009/02/28 10:23:31 UTC
JAXRS : howto produce XML and JSON?
How would you go about creating a jaxrs that can produce both JSON and
XML based on what the caller wants? Do you really need to write the
class twice and annotate each differently?
Re: JAXRS : howto produce XML and JSON?
Posted by hanasaki <ha...@hanaden.com>.
Tried that with CXF 2.1.4... The compiler spits at it. Only one type is
allowed in the annotation...
harinair wrote:
> Seems like this is not a very new thread. I hope you got this figured out. In
> case you didn't or in case somebody else come here... here is the answer:
>
> You annotate it as @Produces({"application/xml","application/json"}) - see
> the value is an array. When the client needs JSON output it need to set
> header parameter "Accept" as "application/json" and call the service. The
> service will return data as JSON. I have also come across Restclient 2.3
> (Available in code.google.com - search in google for Restclient). It is a
> wonderful client to test RESTful webservices.
>
> Hope this helps.
> Hari Gangadharan
>
>
>
> hanasakijiji wrote:
>> The annotation does not seem to like that. Also, how would the caller
>> / invoker specify if they want JSON or XML back?
>>
>> [ERROR]Mojo:
>> [ERROR] org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile
>> [ERROR]FAILED for project:
>> [ERROR] com.hanaden:DemoRestful-WAR:war:1.0-SNAPSHOT
>> [ERROR]Reason:
>> [ERROR]com/service/impl/ServiceImpl.java.java:[23,10] annotation
>> values must be of the form 'name=value'
>> [ERROR]com/service/impl/ServiceImpl.java.java:[23,29] annotation
>> values must be of the form 'name=value'
>>
>>
>> On Sat, Feb 28, 2009 at 11:30 AM, Sergey Beryozkin
>> <sb...@progress.com> wrote:
>>> Hi,
>>> You can do something like
>>>
>>> @Produces("application/xml", "application/json")
>>> public SomeType get() {}
>>>
>>> Cheers, Sergey
>>>
>>> -----Original Message-----
>>> From: hanasaki jiji [mailto:hanasaki@gmail.com]
>>> Sent: 28 February 2009 09:24
>>> To: users@cxf.apache.org
>>> Subject: JAXRS : howto produce XML and JSON?
>>>
>>> How would you go about creating a jaxrs that can produce both JSON and
>>> XML based on what the caller wants? Do you really need to write the
>>> class twice and annotate each differently?
>>>
>>
>
Re: JAXRS : howto produce XML and JSON?
Posted by harinair <ha...@hotmail.com>.
Seems like this is not a very new thread. I hope you got this figured out. In
case you didn't or in case somebody else come here... here is the answer:
You annotate it as @Produces({"application/xml","application/json"}) - see
the value is an array. When the client needs JSON output it need to set
header parameter "Accept" as "application/json" and call the service. The
service will return data as JSON. I have also come across Restclient 2.3
(Available in code.google.com - search in google for Restclient). It is a
wonderful client to test RESTful webservices.
Hope this helps.
Hari Gangadharan
hanasakijiji wrote:
>
> The annotation does not seem to like that. Also, how would the caller
> / invoker specify if they want JSON or XML back?
>
> [ERROR]Mojo:
> [ERROR] org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile
> [ERROR]FAILED for project:
> [ERROR] com.hanaden:DemoRestful-WAR:war:1.0-SNAPSHOT
> [ERROR]Reason:
> [ERROR]com/service/impl/ServiceImpl.java.java:[23,10] annotation
> values must be of the form 'name=value'
> [ERROR]com/service/impl/ServiceImpl.java.java:[23,29] annotation
> values must be of the form 'name=value'
>
>
> On Sat, Feb 28, 2009 at 11:30 AM, Sergey Beryozkin
> <sb...@progress.com> wrote:
>> Hi,
>> You can do something like
>>
>> @Produces("application/xml", "application/json")
>> public SomeType get() {}
>>
>> Cheers, Sergey
>>
>> -----Original Message-----
>> From: hanasaki jiji [mailto:hanasaki@gmail.com]
>> Sent: 28 February 2009 09:24
>> To: users@cxf.apache.org
>> Subject: JAXRS : howto produce XML and JSON?
>>
>> How would you go about creating a jaxrs that can produce both JSON and
>> XML based on what the caller wants? Do you really need to write the
>> class twice and annotate each differently?
>>
>
>
--
View this message in context: http://www.nabble.com/JAXRS-%3A-howto-produce-XML-and-JSON--tp22260215p22469511.html
Sent from the cxf-user mailing list archive at Nabble.com.
Re: JAXRS : howto produce XML and JSON?
Posted by hanasaki jiji <ha...@gmail.com>.
The annotation does not seem to like that. Also, how would the caller
/ invoker specify if they want JSON or XML back?
[ERROR]Mojo:
[ERROR] org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile
[ERROR]FAILED for project:
[ERROR] com.hanaden:DemoRestful-WAR:war:1.0-SNAPSHOT
[ERROR]Reason:
[ERROR]com/service/impl/ServiceImpl.java.java:[23,10] annotation
values must be of the form 'name=value'
[ERROR]com/service/impl/ServiceImpl.java.java:[23,29] annotation
values must be of the form 'name=value'
On Sat, Feb 28, 2009 at 11:30 AM, Sergey Beryozkin
<sb...@progress.com> wrote:
> Hi,
> You can do something like
>
> @Produces("application/xml", "application/json")
> public SomeType get() {}
>
> Cheers, Sergey
>
> -----Original Message-----
> From: hanasaki jiji [mailto:hanasaki@gmail.com]
> Sent: 28 February 2009 09:24
> To: users@cxf.apache.org
> Subject: JAXRS : howto produce XML and JSON?
>
> How would you go about creating a jaxrs that can produce both JSON and
> XML based on what the caller wants? Do you really need to write the
> class twice and annotate each differently?
>
RE: JAXRS : howto produce XML and JSON?
Posted by Sergey Beryozkin <sb...@progress.com>.
Hi,
You can do something like
@Produces("application/xml", "application/json")
public SomeType get() {}
Cheers, Sergey
-----Original Message-----
From: hanasaki jiji [mailto:hanasaki@gmail.com]
Sent: 28 February 2009 09:24
To: users@cxf.apache.org
Subject: JAXRS : howto produce XML and JSON?
How would you go about creating a jaxrs that can produce both JSON and
XML based on what the caller wants? Do you really need to write the
class twice and annotate each differently?