This discussion is archived
0 Replies Latest reply: Jul 1, 2013 5:14 AM by GosalGurpreet RSS

The system could not find a javax.ws.rs.ext.MessageBodyWriter or a DataSourceProvider class for the com.rest.assignment.EmpBean type and application/json mediaType.  Ensure that a javax.ws.rs.ext.MessageBodyWriter exists in the JAX-RS application for the

GosalGurpreet Newbie
Currently Being Moderated

Hi,

Im trying to create a Rest WS with a @GET method that will return me an Emp object. I need the output as a JSON string.

I have created a dynamic web project and added javax RS jars:

When im trying to run this, i'm getting the below mentioned error:

FlushResultHa E org.apache.wink.server.internal.handlers.FlushResultHandler handleResponse The system could not find a javax.ws.rs.ext.MessageBodyWriter or a DataSourceProvider class for the com.rest.assignment.EmpBean type and application/json mediaType.  Ensure that a javax.ws.rs.ext.MessageBodyWriter exists in the JAX-RS application for the type and media type specified.

RequestProces I org.apache.wink.server.internal.RequestProcessor logException The following error occurred during the invocation of the handlers chain: WebApplicationException (500 - Internal Server Error)

Please help as im stuck with this from long.

Thanks in advance.

 

Below is the code for my service class:

 

package com.rest.assignment;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/restService")
public class RestService extends Application {   
   
    @GET
    @Path("/getEmpDetails")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getStringResponse()
    {
        EmpBean empBean = new EmpBean();
        String filePath = "C:/Program Files/IBM/workspace/HelloWorld/src/com/rest/resources/EmpData.properties";
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream(filePath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
       
         Enumeration e = properties.propertyNames();
         String result="";
        String[] empDetailsArr;
        while (e.hasMoreElements()) {
          String key = (String) e.nextElement();
          String empDetails = properties.getProperty(key);
         
          empDetailsArr=empDetails.split(",");    
         
          empBean.setFirstName(empDetailsArr[0]);
          empBean.setLastName(empDetailsArr[1]);
          empBean.setEmpId(empDetailsArr[2]);
          empBean.setDesignation(empDetailsArr[3]);
          empBean.setSkillSet(empDetailsArr[4]);
          result = empDetailsArr[1];
        }
      
        //return empBean;
        return Response.ok(empBean).type(MediaType.APPLICATION_JSON_TYPE).build();

    }   
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<Class<?>>();
        classes.add(RestService.class);
        classes.add(EmpBean.class);
        return classes;
    }

}

 

and my empBean goes like this:

 

package com.rest.assignment;


public class EmpBean {
   
    private String firstName;
    private String lastName;
    private String empId;
    private String designation;
    private String skillSet;
   
   
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getEmpId() {
        return empId;
    }
    public void setEmpId(String empId) {
        this.empId = empId;
    }
    public String getDesignation() {
        return designation;
    }
    public void setDesignation(String designation) {
        this.designation = designation;
    }
    public String getSkillSet() {
        return skillSet;
    }
    public void setSkillSet(String skillSet) {
        this.skillSet = skillSet;
    }
}

 

 

Web.xml goes like this:

 

<?xml version="1.0" encoding="UTF-8"?>

<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<display-name>restWS</display-name>

<welcome-file-list>

  <welcome-file>index.html</welcome-file>

  <welcome-file>index.htm</welcome-file>

  <welcome-file>index.jsp</welcome-file>

  <welcome-file>default.html</welcome-file>

  <welcome-file>default.htm</welcome-file>

  <welcome-file>default.jsp</welcome-file>

</welcome-file-list>

 

<servlet>

  <servlet-name>REST</servlet-name>

  <servlet-class>com.ibm.websphere.jaxrs.server.IBMRestServlet</servlet-class>

  <init-param>

   <param-name>javax.ws.rs.Application</param-name>

   <param-value>com.rest.assignment.RestService</param-value>

  </init-param>

  <load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

  <servlet-name>REST</servlet-name>

  <url-pattern>/rest/*</url-pattern>

</servlet-mapping>

 

</web-app>

 

When i try to return a string from my get method, it gives me a proper response. i get this exception when im trying to return a JSON response.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points