泽西客户端以文本格式返回值,其中制表符分隔多个值

时间:2015-04-11 05:34:15

标签: java json rest jersey jersey-client

RESTfulClient.java

package java4s;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

public class RESTfulClient {

public static void main(String[] Java4s) {

    try {
        Client client = Client.create();    
        WebResource resource = client.resource("http://localhost:cen_neel");    
        ClientResponse response = resource.accept("application/json").get(ClientResponse.class);

        if(response.getStatus() == 200){

            String output = response.getEntity(String.class);
            System.out.println(output); 
            System.out.println(response.getStatus());

        }else System.out.println("Somthing went wrong..!");        

      } catch (Exception e) {    
              e.printStackTrace();    
      }

    }   

}

Customer.java

package java4s;

public class Customer {

private String runID;

public String getRunID() {
    return runID;
}

public void setRunID(String runID) {
    this.runID = runID;
}

public String getTweetID() {
    return tweetID;
}

public void setTweetID(String tweetID) {
    this.tweetID = tweetID;
}

public String getTweet() {
    return tweet;
}

public void setTweet(String tweet) {
    this.tweet = tweet;
}

public int getStartOffset() {
    return StartOffset;
}

public void setStartOffset(int StartOffset) {
    this.StartOffset = StartOffset;
}

public int getEndOffset() {
    return EndOffset;
}

public void setEndOffset(int EndOffset) {
    this.EndOffset = EndOffset;
}

public String getLink() {
    return Link;
}

public void setLink(String Link) {
    this.Link = Link;
}

public String getType() {
    return Type;
}

public void setType(String Type) {
    this.Type = Type;
}
private String tweetID;
private String tweet;
private int StartOffset;
private int EndOffset;
private String Link;
private String Type;

}

JsonFromRestful.java
package java4s;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.zip.GZIPOutputStream;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

@Path("")
public class JsonFromRestful {

@GET
@Path("/{runID}/{tweetID}/{tweet:.+}")
@Produces("application/json")
public Customer produceCustomerDetailsinJSON(@PathParam("runID") String rid,
                                             @PathParam("tweetID") String tid,
                                             @PathParam("tweet") String twt) throws IOException, InterruptedException{

    /*
     * I AM PASSING CUST.NO AS AN INPUT, SO WRITE SOME BACKEND RELATED STUFF AND
     * FIND THE CUSTOMER DETAILS WITH THAT ID. AND FINALLY SET THOSE RETRIEVED VALUES TO
     * THE CUSTOMER OBJECT AND RETURN IT, HOWEVER IT WILL RETURN IN JSON FORMAT :-)
     * */



        Customer cust = new Customer(); 
        Customer cust1=new Customer();
        cust .setRunID(rid);
        cust .setTweetID(tid);
        cust .setTweet(twt);
        cust.setStartOffset(5);
        cust.setEndOffset(10);
        cust.setType("Person");
        cust.setLink("Computer");
        return cust;

}

}

我将从runid开始tweetidtweetusers,我会处理它,然后我会返回结果。

每个给定的输入可能包含许多Link,type,startoffset和endoffset。例如,如果我将输入作为s1,d1,计算机和输出可能是:

  

S1,D1,计算机,5(开始偏移),10(endoffset),计算机(链接),机器(类型):   第一个值和第二个值可以是> s1,d1,计算机,8(startoffset),11(endoffset),pc(链接),机器(类型):

所以我 需要以文本格式向用户返回所有值,其中制表符分隔的值不是json格式 (目前我正在输出以json格式,它将只返回一个值)。

如何解决此问题?

0 个答案:

没有答案