2014-12-19 267 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