繁体   English   中英

如何在球衣上发送对象作为对休息的响应

How to send object as response on rest in jersey

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文繁体   英文版本   中英对照 版本,有任何建议请联系yoyou2525@163.com。

我有一个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类

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 个回复

检查是否在Jersery功能的类路径中包含了所有必需的依赖项。 我想您已经错过了Jackson功能:

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

6 球衣休息异步响应

我正在尝试从下面的方法中立即获取响应,并继续进行其余的过程。 但是我什么时候都看不到我的返回值“ TestAync”。 您能否建议如何从以下电话中检索(客户端代码)响应。 这是我正在拨打的客户电话,感谢您的帮助。 将来的响应= ClientBuilder.newClient()。ta ...

7 球衣休息和csv响应

我创建了一个休息电话,该电话使用泽西岛(Jersey)响应CSV文件。 其余呼叫代码为: 上面的代码读取在临时位置创建的csv文件,并使用rest调用响应该csv。 这是完美的工作。 但是现在要求已更改。 将文件内容流式传输到内存中,然后从Rest API以csv格式进行响应 ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2022 STACKOOM.COM