POJO对象未转换为JSON对象

时间:2014-12-19 03:29:39

标签: java json jersey

我正在尝试编写一个REST服务,它将获取二进制数据并返回JSON OBject。

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>

1 个答案:

答案 0 :(得分:0)

Haven未经过测试,但最有可能的原因是您正在打印Result对象,这将打印toString。如果您需要原始JSON,请使用response.getEntity(String.class)。使用getEntity(Result.class)时,原始JSON将转换为Result对象。