Apache Wink Json REST Web Service

860 views Asked by At

How can I make Apache Wink to return something like

{ Message: "Hello World!" }

I have the following code:

@Asset
public class Hello {
    protected String message;
    public Hello() {
    }

    @Produces(MediaType.APPLICATION_JSON)
    public String getMessage() {
        return message;
    }

    @Consumes(MediaType.APPLICATION_JSON)
    public void setMessage(String message) {
        this.message = message;
    }
}

@Path("/helloworld")
public class HelloWorldResource {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Hello getMessage() {
        Hello hello = new Hello();
        hello.setMessage("Hello World!");

        return hello;
    }
}

and the server returns only "Hello World!". How can I make it return JSON that have a structure similar with the java class?

2

There are 2 answers

0
Tarlog On

Hello is not an asset, it's a domain class. So you don't need to annotate it.

Also make sure that you have some json support (I think having Jackson is preferred). By default there is no JSON provided registered.

0
Meru On

I do not know what exactly you mean with 'that have a structure similar with the java class'. If you refer to wanting to return a json structure I can add following: To return JSON I used: @Produces("application/json") And I parsed the string to JSON-format.