简体   繁体   中英

POJO Object not converting to JSON Object

Im trying to write a REST service that will take a binary data and return JSON OBject.

REST Service:

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;

    }
}

RESULT OBject

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

I'm not sure why its not printing the output as JSON Object. This looks like a raw String.

Error when I print the String.class directly -

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>

Haven't tested, but it's most likely due to the fact that you are printing the Result object, which will print the toString . If you want the raw JSON, then use response.getEntity(String.class) . When you use getEntity(Result.class) , then the raw JSON is converted to a Result object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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