Java – returns the JSON representation of a string with Jersey
I'm about to set up a rest - Web service with Jersey
@GET @Path("user") @Produces( MediaType.APPLICATION_JSON) public User user() { return new User("reini","admin"); }
Calling this method in the browser will display a "good" JSON string:
{"name":"reini","role":"admin"}
My second method is as follows:
@GET @Path("hello/{name}") @Produces( MediaType.APPLICATION_JSON) public String hello(@PathParam("name") String name) { return "Hello " + name + ",it is " + new Date(); }
Calling this method in 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 use this service with the dojo toolkit The problem is that once I set the [handleas: "JSON"] – flag, I get the second method It gives me an error "syntax error: 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 comments do I generate for my methods?
Solution
You should define a dto and put your string there So you will use a string as an attribute to generate a helloresp class Fill in the property in your method and return
You can view this tutorial Another tutorial
Firefox does not display errors because it does not process your response Displays anything returned by the service However, the toolkit starts to process the response as JSON, but there is no valid JSON (JSON starts with {)