Returning the JSON representation of a String with Jersey

11,354

Solution 1

You should define a DTO and put your String in that. So you will hava a HelloResp class with one String as attribute. In your method populate that attribute and return.

You can check this Tutorial. Another tutorial.

Firefox is not showing error because, it is not processing your response. Whatever is returned by service is displayed. The toolkit however starts processing the reponse as a JSON but did not a valid JSON (JSON starts with {)

Solution 2

If you are returning a String why do you define it as a type JSON?

Just return it as a plain text (MediaType.TEXT_PLAIN):

@GET
@Path("hello/{name}")
@Produces( MediaType.TEXT_PLAIN)
public String hello(@PathParam("name") String name) {
    return "Hello " + name + ", it is " + new Date();
}

Solution 3

You can also return it as:

@GET
@Path("hello/{name}")
@Produces( MediaType.APPLICATION_JSON)
public String hello(@PathParam("name") String name) {
    return "\"Hello " + name + ", it is " + new Date()+'"';
}

but it's look very strange for me.

Creating DTO for every object also looks strange just for one String.

Is there any better option?

Share:
11,354

Related videos on Youtube

Reini
Author by

Reini

Computer Science Student

Updated on September 18, 2022

Comments

  • Reini
    Reini over 1 year

    I'm about to setup a REST-Webservice with Jersey. At the moment I am a bit confused about the correct representation of Strings or other Value types in JSON. Here are two snippets:

    @GET
    @Path("user")
    @Produces( MediaType.APPLICATION_JSON)
    public User user() {
        return new User("reini", "admin");
    }
    

    Calling this method in a Browser will display a "good" JSON String like that:

    {"name":"reini","role":"admin"}
    

    My second method looks like this:

    @GET
    @Path("hello/{name}")
    @Produces( MediaType.APPLICATION_JSON)
    public String hello(@PathParam("name") String name) {
        return "Hello " + name + ", it is " + new Date();
    }
    

    Calling that method in a Browswer will display a pure String without any JSON-Stuff (Curly Braces etc):

    Hello firefox, it is Tue Sep 18 13:52:57 CEST 2012
    

    I want to consume this service with the dojo toolkit. The problem is, that I get an for the second method as soon as I set [handleAs: "json"]-flag. It throws me an error "SyntaxError: Unexpected token H" where "H" is the first letter of the returned string.

    So: What is the correct json representation of Strings and other value types and what annotations I have to set for my method to produce these?

  • Reini
    Reini over 11 years
    is it better to create a DTO for every operation or to create a DTO for each type I want to use?
  • ddumont
    ddumont almost 9 years
    "JSON starts with {" - Incorrect. According to json.org even a quoted string literal is valid JSON, and jersey should support correctly serializing a string as a JSON string.
  • user1386966
    user1386966 over 7 years
    Can you please explain why you are using a double quote? ("") instead of just : return "Something" ? I'm having an issue with that and dont understand why a regular string is not converting to a valid json response
  • SiMet
    SiMet over 7 years
    Reason is that this returns MediaType Application Json and you could manualy return some json as "{\"a\": 1}" which is not a string. See aproved answer