问题描述:

I have a ResponseModel class, I want to send it's object in response to rest. But I am getting this error:

A message body writer for Java class

com.tcs.srl.message.model.ResponseModel, and Java type class

com.tcs.srl.message.model.ResponseModel, and MIME media type

application/json was not found.

I can not use Maven in my project.So can not add jersey dependency in pom.xml.

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;

import javax.ws.rs.Consumes;

import javax.ws.rs.POST;

import javax.ws.rs.Path;

import javax.ws.rs.Produces;

import javax.ws.rs.core.*;

import javax.ws.rs.core.Response.ResponseBuilder;

import javax.xml.bind.annotation.XmlRootElement;

/*import org.codehaus.jackson.JsonGenerationException;

import org.codehaus.jackson.map.JsonMappingException;

import org.codehaus.jackson.map.ObjectMapper;*/

import org.json.simple.JSONObject;

import org.json.simple.parser.JSONParser;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import com.tcs.DataShare.Client.DataShareServiceClient;

import com.tcs.DataShare.Client.GetFilesOnLocal;

import com.tcs.DataShare.Constants.DataShareConstants;

import com.tcs.srl.message.model.ResponseModel;

import com.sun.jersey.api.client.Client;

import com.sun.jersey.api.client.ClientResponse;

@Path("/ConfigureAndrew")

@XmlRootElement

public class ConfigureAndrew {

private static final Logger LOGGER = LoggerFactory

.getLogger(ConfigureAndrew.class);

private ClientResponse response;

@Path("/configureAndrew")

@POST

@Produces(MediaType.APPLICATION_JSON)

public ResponseModel configureAndrew(@Context HttpServletRequest requestObj,

String serviceData) {

LOGGER.debug("Inside Configure Andrew web service: ");

JSONParser parser = new JSONParser();

String isSuccess = "false";

ResponseModel result=null;

try {

result = new ResponseModel();

String headerString = requestObj.getHeader("header");

JSONObject serviceJSON = (JSONObject) parser.parse(serviceData);

String datashareURL = (String) serviceJSON

.get(DataShareConstants.DATASHARE_URL);

String datasharePort = (String) serviceJSON

.get(DataShareConstants.DATASHARE_PORT);

System.out.println("datasharePort: " + datasharePort);

String servicename = (String) serviceJSON

.get(DataShareConstants.SERVICE_NAME);

String appname = (String) serviceJSON

.get(DataShareConstants.APP_NAME);

String version = (String) serviceJSON

.get(DataShareConstants.APP_VERSION);

String path = (String) serviceJSON.get(DataShareConstants.APP_PATH);

DataShareServiceClient dataShareServiceClient = new DataShareServiceClient(

datashareURL, datasharePort, servicename);

InputStream inputStream = dataShareServiceClient

.zipFolderWithSubsequestFolder(path);

if (inputStream.available() != 0) {

System.out.println("In the input stream");

GetFilesOnLocal getFilesOnLocal = new GetFilesOnLocal();

result = getFilesOnLocal.getFiles(inputStream, result);

LOGGER.debug("Input stream contains data");

} else {

LOGGER.error("Input stream is empty");

}

LOGGER.debug("DatashareServices :completed configure Andrew");

isSuccess = "true";

result.setSuccess("true");

result.setErrors(null);

result.setData("data is to be added");

} catch (Exception e) {

e.printStackTrace();

LOGGER.error("DatashareServices :configureAndrew service failed "

+ e);

isSuccess = "false";

result.getErrors().add("Error in configureAndrew method");

}

System.out.println("result is"+result.getSuccess());

JSONObject res=new JSONObject();

return result;

}

}

ResponseModel POJO Class

package com.tcs.ngps.sip.model;

import java.io.Serializable;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

public class ResponseModel implements Serializable{

String success;

List errors;

public String getSuccess() {

return success;

}

public void setSuccess(String success) {

this.success = success;

}

public List getErrors() {

return errors;

}

public void setErrors(List errors) {

this.errors = errors;

}

public List getWarnings() {

return warnings;

}

public void setWarnings(List warnings) {

this.warnings = warnings;

}

public Object getData() {

return data;

}

public void setData(Object data) {

this.data = data;

}

List warnings;

Object data;

String transactionId;

public String getTransactionId() {

return transactionId;

}

public void setTransactionId(String transactionId) {

this.transactionId = transactionId;

}

public ResponseModel(){

this.errors = new ArrayList<>();

this.warnings = new ArrayList<>();

this.data = new HashMap<>();

}

public ResponseModel(String success, List errors, List warnings,

HashMap data,String transactionId) {

super();

this.success = success;

this.errors = errors;

this.warnings = warnings;

this.data = data;

this.transactionId=transactionId;

}

}

web.xml

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

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

id="WebApp_ID" version="2.5">

<display-name>DataShareWebService</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>Jersey REST Service</servlet-name>

<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

<init-param>

<param-name>com.sun.jersey.config.property.packages</param-name>

<param-value>com.tcs.DataShare.WebService</param-value>

</init-param>

<init-param>

<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>

<param-value>true</param-value>

</init-param>

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

</servlet>

<servlet-mapping>

<servlet-name>Jersey REST Service</servlet-name>

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

</servlet-mapping>

</web-app>

网友答案:

Check if you have included all the required dependencies in your classpath for the Jersery features. I think you have missed to include Jackson Features:

Check if you have jersey-media-json-jackson-<required-vesrion>.jar added in your classpath.

相关阅读:
Top