如何在球衣上发送对象作为回应

时间:2015-03-31 05:33:56

标签: json web-services rest jersey-1.0

我有一个ResponseModel类,我想发送它的对象以响应休息。但是我收到了这个错误:

  

Java类的消息正文编写器   com.tcs.srl.message.model.ResponseModel和Java类型类   com.tcs.srl.message.model.ResponseModel和MIME媒体类型   找不到application / json。

我无法在我的项目中使用Maven。因此无法在pom.xml中添加jersey依赖项。

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>

1 个答案:

答案 0 :(得分:0)

检查是否在Jersery功能的类路径中包含了所有必需的依赖项。我想你错过了杰克逊特色:

检查您的类路径中是否添加了jersey-media-json-jackson-<required-vesrion>.jar