1

I am trying to deserialize an object send with postman using gson.fromJson.

 @POST
    @Path( "company" ) // takes name as a path parameter
    @Produces( "application/json" ) // response formatted as JSON
    public Response insertCompany(String content) {
        Gson gson = new Gson();
        CompanyWithNoIdReturn newCompany = gson.fromJson(content,  CompanyWithNoIdReturn.class);
   }

public class CompanyWithNoIdReturn {

     private final String name;
     private final String description;
     private final String logo;

    public CompanyWithNoIdReturn(String name, String description, String logo) 
    {
        this.name = name;
        this.description = description;
        this.logo = logo;
    }

    public String getName() {

        return name;
    }

    public String getDescription() {

        return description;
    }

    public String getLogo() {

        return logo;
    }

}

The error that comes up is: javax.servlet.ServletException:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1

I understand that it means that it expects it to begin with { and not "as mentioned in here. Though what i have not understood is how do i fix it?

The json was contructed like this: enter image description here

EDIT: While using form - data the content is like:

content = (java.lang.String) "------WebKitFormBoundaryUG5yy1gW9G2V5qcz
Content-Disposition: form-data; name="CompanyName"

newCompanyName2
------WebKitFormBoundaryUG5yy1gW9G2V5qcz
Content-Disposition: form-data; name="Description"

newDescription2
------WebKitFormBoundaryUG5yy1gW9G2V5qcz
Content-Disposition: form-data; name="logo"

newLogo2
------WebKitFormBoundaryUG5yy1gW9G2V5qcz--
"
Community
  • 1
  • 1
J.mp
  • 81
  • 5

2 Answers2

0

I think you expect body (content variable) in JSON format. But according to your image of postman your are sending form-data.

Try to switch to raw and insert this:

{
    "name": "Company name",
    "description": "Company description",
    "logo": "logo"
}
Ilya Tretyakov
  • 6,622
  • 3
  • 26
  • 44
  • You are correct. It works fine. Though if i wanted it to work with form - data or xxx-www-form-urlencoded how could this be done? – J.mp Sep 28 '15 at 07:12
  • If you are using framework it make sence to look into documetation. It should have classes for it. But I don't know what you are using and could not recommend anything. But in case of `form-data` your `content` should be similar to "name=a&description=b&logo=c" and you can parse it using `String` class. – Ilya Tretyakov Sep 28 '15 at 07:36
  • I am not using any framework though i did not understand. If i use the code as in my initial question it produces an error cause it expects an object not a String. Can you write the code processing the data as i have written in my initial question? – J.mp Sep 29 '15 at 06:13
  • post the value of your `content` variable – Ilya Tretyakov Sep 29 '15 at 09:10
0

If you want to process form data with a JaxRs service, you should look at @FormParam. Your service would become

@POST
@Path( "company" ) // takes name as a path parameter
@Produces( "application/json" ) // response formatted as JSON
public Response insertCompany(@FormParam("CompanyName") String name, @FormParam("Description") String description, @FormParam("Logo") String logo) ) {
    CompanyWithNoIdReturn newCompany = new CompanyWithNoIdReturn(name, description, logo);
    return Response.ok(newCompany).build();
}

You have to ensure that the names specified as in the FormParam annotations match the fieldnames in your form.

Maarten Winkels
  • 2,317
  • 15
  • 14