2014-12-19 263 views
2

我試圖編寫一個REST服務,它將採取二進制數據並返回JSON對象。POJO對象不轉換爲JSON對象

REST服務:

public class FileUpload { 

    @POST 
    @Path("/upload") 
    @Consumes(MediaType.APPLICATION_OCTET_STREAM) 
    @Produces(MediaType.APPLICATION_JSON) 
    public Result uploadFile(InputStream uploadedInputStream) { 
     String uploadedFileLocation = "c://tomcatupload/" + filename; 

     // save it 
     writeToFile(uploadedInputStream, uploadedFileLocation); 

     String output = "File uploaded to : " + uploadedFileLocation; 

     //return Response.status(200).entity(output).build(); 
     Result result = new Result("status","success"); 

     return result; 

    } 
} 

結果對象

import javax.xml.bind.annotation.XmlRootElement; 

public class Result { 
    private String Status; 
    private String result; 
    public Result(){ 

    } 
    public Result(String status, String result){ 
     this.Status = status; 
     this.result = result; 
    } 
    public String getStatus() { 
     return Status; 
    } 
    public void setStatus(String status) { 
     Status = status; 
    } 
    public String getResult() { 
     return result; 
    } 
    public void setResult(String result) { 
     this.result = result; 
    } 
    @Override 
    public String toString(){ 
     return new StringBuffer("result : ").append(this.result).toString(); 
    } 

} 


public class FileUploadClient { 
    public static void main(String[] args) throws JSONException{ 
     Client client = Client.create(); 
     WebResource webResource = client 
        .resource("http://localhost:8080/RESTFileAttachmentService/rest/file/upload"); 
     String fileName = "C:/test.txt"; 
     InputStream fileInStream = null; 
     try { 
      fileInStream = new FileInputStream(fileName); 
     } catch (FileNotFoundException e) { 
      System.out.println("File not found exception"); 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     //String sContentDisposition = "attachment; filename=\"" + fileName.getName()+"\""; 
     //WebResource fileResource = a_client.resource(a_sUrl); 
     System.out.println("Webservice is being called = " + fileInStream); 
     ClientResponse response = webResource.type(MediaType.APPLICATION_OCTET_STREAM).accept("application/json") 
           .post(ClientResponse.class, fileInStream); 
     System.out.println("Webservice call over = " + response.getStatus()); 
     System.out.println("Webservice call over = " + response.getEntity(Result.class)); 

    } 
} 

Ouptut:

result : success 

我不知道爲什麼它不打印輸出爲JSON對象。這看起來像一個原始字符串。

錯誤,當我直接打印的String.class -

Exception in thread "main" javax.ws.rs.WebApplicationException: com.owlike.genson.JsonBindingException: Could not deserialize to type class java.lang.String 
    at com.owlike.genson.ext.jaxrs.GensonJsonConverter.readFrom(GensonJsonConverter.java:127) 
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:553) 
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:506) 
    at com.gsa.gov.file.upload.FileUploadClient.main(FileUploadClient.java:37) 

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>esoa</groupId> 
    <artifactId>RESTFileAttachmentService</artifactId> 
    <packaging>war</packaging> 
    <version>0.0.1-SNAPSHOT</version> 
    <name>RESTFileAttachmentService Maven Webapp</name> 
    <url>http://maven.apache.org</url> 
    <dependencies> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>3.8.1</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>com.sun.jersey</groupId> 
      <artifactId>jersey-server</artifactId> 
      <version>1.8</version> 
     </dependency> 
     <dependency> 
      <groupId>com.sun.jersey.contribs</groupId> 
      <artifactId>jersey-multipart</artifactId> 
      <version>1.8</version> 
     </dependency> 
     <dependency> 
      <groupId>com.sun.jersey</groupId> 
      <artifactId>jersey-client</artifactId> 
      <version>1.8</version> 

     </dependency> 
     <!-- <dependency> <groupId>org.jboss.spec.javax.servlet</groupId> <artifactId>jboss-servlet-api_3.0_spec</artifactId> 
      <version>1.0.2.Final</version> </dependency> --> 
     <dependency> 
      <groupId>com.sun.jersey</groupId> 
      <artifactId>jersey-bundle</artifactId> 
      <version>1.18.1</version> 
     </dependency> 

     <dependency> 
      <groupId>com.owlike</groupId> 
      <artifactId>genson</artifactId> 
      <version>0.99</version> 
     </dependency> 

     <dependency> 
      <groupId>com.sun.jersey</groupId> 
      <artifactId>jersey-json</artifactId> 
      <version>1.9</version> 
      <scope>provided</scope> 
     </dependency> 


    </dependencies> 
    <repositories> 
     <repository> 
      <id>maven2-repository.java.net</id> 
      <name>Java.net Repository for Maven</name> 
      <url>http://download.java.net/maven/2/</url> 
      <layout>default</layout> 
     </repository> 
    </repositories> 

    <build> 
     <finalName>RESTFileAttachmentService</finalName> 
    </build> 
</project> 
+0

你說的是JSON,但是你用它對XML進行了註釋? – Makoto 2014-12-19 03:31:21

+0

我是否應該明確地將引號添加到看起來像JSON對象或轉換應該自動發生? – user1050619 2014-12-19 03:42:28

回答

0

沒有測試,但它是最有可能由於這樣的事實,你要打印的Result對象,它將打印toString。如果你想要原始的JSON,然後使用response.getEntity(String.class)。當您使用getEntity(Result.class)時,原始JSON將轉換爲Result對象。

+0

你可能是對的,但當我顯示字符串時,我得到了一個不同的錯誤。我已經給出了錯誤。 – user1050619 2014-12-19 03:47:50

+0

我沒有(或者真的)使用Genson作爲提供者。你能發佈Maven依賴項嗎?另外,如果需要以任何方式配置Genson,您是否可以發佈這些配置,以便我可以測試。似乎是一個與Genson的問題,因爲我從來沒有任何問題,這與普通的JSON提供者 – 2014-12-19 03:54:27

+0

如果我不使用Genson,我會得到一個不同的錯誤,即;它無法寫入JSON響應。 – user1050619 2014-12-19 03:59:19