You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Pankaj Jangid <pa...@gmail.com> on 2011/06/14 18:17:58 UTC

No message body writer has been found for response class HashMap.

I am new to CXF.
I want to GET result as JSON. My getCustomer() resource method is like this

@Path("/")
@Produces("application/json,application/xml")
public class Service
{
  @GET
  @Path("/getCustomers")
  public String getCustomers() {
    HashMap hm = new HashMap();
    hm.put("Hello", "World");
    return hm;
}

my bean.xml (which is referenced in web.xml is

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:jaxrs="http://cxf.apache.org/jaxrs"
  xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://cxf.apache.org/jaxrs
  http://cxf.apache.org/schemas/jaxrs.xsd">

  <import resource="classpath:META-INF/cxf/cxf.xml" />
  <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

  <jaxrs:server id="rest-api" address="/myservice">
    <jaxrs:serviceBeans>
      <ref bean="serviceBean"/>
    </jaxrs:serviceBeans>
    <jaxrs:extensionMappings>
      <entry key="json" value="application/json"/>
      <entry key="xml" value="application/xml"/>
    </jaxrs:extensionMappings>
    <jaxrs:providers>
      <ref bean="jaxbProvider"/>
      <ref bean="jsonProvider"/>
    </jaxrs:providers>
  </jaxrs:server>

  <bean id="serviceBean"
    class="my.Service"/>
  <bean id="jaxbProvider"
    class="org.apache.cxf.jaxrs.provider.JAXBElementProvider"/>
  <bean id="jsonProvider"
    class="org.apache.cxf.jaxrs.provider.JSONProvider"/>

</beans>

When I access the service it produces this message:-

No message body writer has been found for response class HashMap.

--
Regards
Pankaj

Re: No message body writer has been found for response class HashMap.

Posted by Sergey Beryozkin <sb...@gmail.com>.
This is because multiple media types should be specified like this:

@Produces({"application/json","application/xml"})
@Consumes({"application/json","application/xml"})

Cheers, Sergey

On Wed, Jun 15, 2011 at 4:52 PM, Pankaj Jangid <pa...@gmail.com> wrote:
> Changed the service class to:
>
>
> @Path("/")
> @Produces("application/json,application/xml")
> @Consumes("application/json,application/xml")
> public class Service
> {
>  @GET
>  @Path("/getCustomer")
>  public JAXBElement<GreetingListType> getCustomer() {
>    ObjectFactory of = new ObjectFactory();
>    GreetingListType grList = of.createGreetingListType();
>    GreetingType g = of.createGreetingType();
>    g.setText("Hello World");
>    g.setLanguage("en");
>    grList.getGreeting().add(g);
>    JAXBElement<GreetingListType> gl = of.createGreetings( grList );
>    return gl;
> }
>
> It is now producing this message:
>
> No message body writer has been found for response class JAXBElement.
>
>
> --
> Regards
> Pankaj
>
> On 14 June 2011 22:14, Sergey Beryozkin <sb...@gmail.com> wrote:
>
>> I'd recommend you avoid returning Map directly and wrap it inside
>> custom JAXB bean,
>> Otherwise you need to add XmlJavaTypeAdaper. May be Jackson can handle
>> Map directly - not sure about it
>> Cheers, Sergey
>>
>> On Tue, Jun 14, 2011 at 5:17 PM, Pankaj Jangid <pa...@gmail.com>
>> wrote:
>> > I am new to CXF.
>> > I want to GET result as JSON. My getCustomer() resource method is like
>> this
>> >
>> > @Path("/")
>> > @Produces("application/json,application/xml")
>> > public class Service
>> > {
>> >  @GET
>> >  @Path("/getCustomers")
>> >  public String getCustomers() {
>> >    HashMap hm = new HashMap();
>> >    hm.put("Hello", "World");
>> >    return hm;
>> > }
>> >
>> > my bean.xml (which is referenced in web.xml is
>> >
>> > <beans xmlns="http://www.springframework.org/schema/beans"
>> >  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>> >  xmlns:jaxrs="http://cxf.apache.org/jaxrs"
>> >  xsi:schemaLocation="
>> >  http://www.springframework.org/schema/beans
>> >  http://www.springframework.org/schema/beans/spring-beans.xsd
>> >  http://cxf.apache.org/jaxrs
>> >  http://cxf.apache.org/schemas/jaxrs.xsd">
>> >
>> >  <import resource="classpath:META-INF/cxf/cxf.xml" />
>> >  <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
>> >
>> >  <jaxrs:server id="rest-api" address="/myservice">
>> >    <jaxrs:serviceBeans>
>> >      <ref bean="serviceBean"/>
>> >    </jaxrs:serviceBeans>
>> >    <jaxrs:extensionMappings>
>> >      <entry key="json" value="application/json"/>
>> >      <entry key="xml" value="application/xml"/>
>> >    </jaxrs:extensionMappings>
>> >    <jaxrs:providers>
>> >      <ref bean="jaxbProvider"/>
>> >      <ref bean="jsonProvider"/>
>> >    </jaxrs:providers>
>> >  </jaxrs:server>
>> >
>> >  <bean id="serviceBean"
>> >    class="my.Service"/>
>> >  <bean id="jaxbProvider"
>> >    class="org.apache.cxf.jaxrs.provider.JAXBElementProvider"/>
>> >  <bean id="jsonProvider"
>> >    class="org.apache.cxf.jaxrs.provider.JSONProvider"/>
>> >
>> > </beans>
>> >
>> > When I access the service it produces this message:-
>> >
>> > No message body writer has been found for response class HashMap.
>> >
>> > --
>> > Regards
>> > Pankaj
>> >
>>
>



-- 
Sergey Beryozkin

Application Integration Division of Talend
http://sberyozkin.blogspot.com

Re: No message body writer has been found for response class HashMap.

Posted by Pankaj Jangid <pa...@gmail.com>.
Changed the service class to:


@Path("/")
@Produces("application/json,application/xml")
@Consumes("application/json,application/xml")
public class Service
{
  @GET
  @Path("/getCustomer")
  public JAXBElement<GreetingListType> getCustomer() {
    ObjectFactory of = new ObjectFactory();
    GreetingListType grList = of.createGreetingListType();
    GreetingType g = of.createGreetingType();
    g.setText("Hello World");
    g.setLanguage("en");
    grList.getGreeting().add(g);
    JAXBElement<GreetingListType> gl = of.createGreetings( grList );
    return gl;
}

It is now producing this message:

No message body writer has been found for response class JAXBElement.


--
Regards
Pankaj

On 14 June 2011 22:14, Sergey Beryozkin <sb...@gmail.com> wrote:

> I'd recommend you avoid returning Map directly and wrap it inside
> custom JAXB bean,
> Otherwise you need to add XmlJavaTypeAdaper. May be Jackson can handle
> Map directly - not sure about it
> Cheers, Sergey
>
> On Tue, Jun 14, 2011 at 5:17 PM, Pankaj Jangid <pa...@gmail.com>
> wrote:
> > I am new to CXF.
> > I want to GET result as JSON. My getCustomer() resource method is like
> this
> >
> > @Path("/")
> > @Produces("application/json,application/xml")
> > public class Service
> > {
> >  @GET
> >  @Path("/getCustomers")
> >  public String getCustomers() {
> >    HashMap hm = new HashMap();
> >    hm.put("Hello", "World");
> >    return hm;
> > }
> >
> > my bean.xml (which is referenced in web.xml is
> >
> > <beans xmlns="http://www.springframework.org/schema/beans"
> >  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> >  xmlns:jaxrs="http://cxf.apache.org/jaxrs"
> >  xsi:schemaLocation="
> >  http://www.springframework.org/schema/beans
> >  http://www.springframework.org/schema/beans/spring-beans.xsd
> >  http://cxf.apache.org/jaxrs
> >  http://cxf.apache.org/schemas/jaxrs.xsd">
> >
> >  <import resource="classpath:META-INF/cxf/cxf.xml" />
> >  <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
> >
> >  <jaxrs:server id="rest-api" address="/myservice">
> >    <jaxrs:serviceBeans>
> >      <ref bean="serviceBean"/>
> >    </jaxrs:serviceBeans>
> >    <jaxrs:extensionMappings>
> >      <entry key="json" value="application/json"/>
> >      <entry key="xml" value="application/xml"/>
> >    </jaxrs:extensionMappings>
> >    <jaxrs:providers>
> >      <ref bean="jaxbProvider"/>
> >      <ref bean="jsonProvider"/>
> >    </jaxrs:providers>
> >  </jaxrs:server>
> >
> >  <bean id="serviceBean"
> >    class="my.Service"/>
> >  <bean id="jaxbProvider"
> >    class="org.apache.cxf.jaxrs.provider.JAXBElementProvider"/>
> >  <bean id="jsonProvider"
> >    class="org.apache.cxf.jaxrs.provider.JSONProvider"/>
> >
> > </beans>
> >
> > When I access the service it produces this message:-
> >
> > No message body writer has been found for response class HashMap.
> >
> > --
> > Regards
> > Pankaj
> >
>

Re: No message body writer has been found for response class HashMap.

Posted by Sergey Beryozkin <sb...@gmail.com>.
I'd recommend you avoid returning Map directly and wrap it inside
custom JAXB bean,
Otherwise you need to add XmlJavaTypeAdaper. May be Jackson can handle
Map directly - not sure about it
Cheers, Sergey

On Tue, Jun 14, 2011 at 5:17 PM, Pankaj Jangid <pa...@gmail.com> wrote:
> I am new to CXF.
> I want to GET result as JSON. My getCustomer() resource method is like this
>
> @Path("/")
> @Produces("application/json,application/xml")
> public class Service
> {
>  @GET
>  @Path("/getCustomers")
>  public String getCustomers() {
>    HashMap hm = new HashMap();
>    hm.put("Hello", "World");
>    return hm;
> }
>
> my bean.xml (which is referenced in web.xml is
>
> <beans xmlns="http://www.springframework.org/schema/beans"
>  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>  xmlns:jaxrs="http://cxf.apache.org/jaxrs"
>  xsi:schemaLocation="
>  http://www.springframework.org/schema/beans
>  http://www.springframework.org/schema/beans/spring-beans.xsd
>  http://cxf.apache.org/jaxrs
>  http://cxf.apache.org/schemas/jaxrs.xsd">
>
>  <import resource="classpath:META-INF/cxf/cxf.xml" />
>  <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
>
>  <jaxrs:server id="rest-api" address="/myservice">
>    <jaxrs:serviceBeans>
>      <ref bean="serviceBean"/>
>    </jaxrs:serviceBeans>
>    <jaxrs:extensionMappings>
>      <entry key="json" value="application/json"/>
>      <entry key="xml" value="application/xml"/>
>    </jaxrs:extensionMappings>
>    <jaxrs:providers>
>      <ref bean="jaxbProvider"/>
>      <ref bean="jsonProvider"/>
>    </jaxrs:providers>
>  </jaxrs:server>
>
>  <bean id="serviceBean"
>    class="my.Service"/>
>  <bean id="jaxbProvider"
>    class="org.apache.cxf.jaxrs.provider.JAXBElementProvider"/>
>  <bean id="jsonProvider"
>    class="org.apache.cxf.jaxrs.provider.JSONProvider"/>
>
> </beans>
>
> When I access the service it produces this message:-
>
> No message body writer has been found for response class HashMap.
>
> --
> Regards
> Pankaj
>