Friday, November 18, 2016

REST webservice - Apache CXF converts String to JSON incorrectly

I have created a simple REST webservice and it returns a String value from Response. But browser givesan error and nothing shows from tomcat side.


Error shown in font end: This is basically generic error

=======================

This page contains the following errors:

error on line 1 at column 1: Document is empty

Below is a rendering of the page up to the first error.

=============================


below is the coding for my webservice

------------------------------------------------------------------------------

@Path("/")
@WebService(name = "mailService", targetNamespace = "http://localhost/cxf-rest/example")
public interface MailService {

@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("/testMail")
public Response sendTestMail(@QueryParam("env") String env);

}

--------------------------------------------------------------------

----------------------------------------------------------------------------------------

public class MailServiceImpl implements MailService {


@Override
public Response sendTestMail(String env) {
// TODO Auto-generated method stub
return Response.ok("good").build();
}
}


----------------------------------------------------------------------------------------

below is the definitions for 'beans.xml"

------------------------------------------------------------------------------------------

<jaxrs:server id="mailService" address="/mailservices">
<jaxrs:providers>
<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
</jaxrs:providers>
<jaxrs:serviceBeans>
<ref bean="mailServiceImpl" />
</jaxrs:serviceBeans>
<jaxrs:extensionMappings>
<entry key="xml" value="application/xml" />
<entry key="json" value="application/json" />
</jaxrs:extensionMappings>
</jaxrs:server>
<bean id="mailServiceImpl" class="com.rest.cxfrestservice.service.MailServiceImpl" />

-----------------------------------------------------------------------------------------------

Reason
Jaxb does not know how to convert String to Json.

you have few solutions

Solution 1:
change response type to "text/plain"


----------------------------------------------------------------------------------------

@Path("/")
@WebService(name = "mailService", targetNamespace = "http://localhost/cxf-rest/example")
public interface MailService {


@GET
@Produces("text/plain")
@Path("/testMail")
public Response sendTestMail(@QueryParam("env") String env);

}
----------------------------------------------------------------------------------------


this will solve the error. But output will be a plain text.

Solution 2 : 
Use wrapper

----------------------------------------------------------------------------------------

@XmlRootElement(name = "statusMessage")
public class StatusMessage {

private String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

}

----------------------------------------------------------------------------------------

new Service Impl class

---------------------------------------------------------------------------------------

@Path("/")
@WebService(name = "mailService", targetNamespace = "http://localhost/cxf-rest/example")
public interface MailService {

@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("/sendMail")
public Response sendRefreshMail(@QueryParam("env") String env);

}

------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
public class MailServiceImpl implements MailService {

public Response sendRefreshMail(String env) {
if (env == null) {
return Response.status(Response.Status.BAD_REQUEST).build();
}
StatusMessage statusMessage = new StatusMessage();
statusMessage.setMessage(mailSao.sendEmail(env));

return Response.ok(statusMessage).build();
}


}


----------------------------------------------------------------------------------------


This will return response as needed. below is xml output

<statusMessage>
<message>SUCCESS</message>
</statusMessage>

below link is a good one

No comments:

Post a Comment