简体   繁体   English

Apache Wink Json REST Web服务

[英]Apache Wink Json REST Web Service

How can I make Apache Wink to return something like 如何使Apache Wink返回类似

{ 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!". 并且服务器仅返回“ Hello World!”。 How can I make it return JSON that have a structure similar with the java class? 如何使其返回与Java类具有相似结构的JSON?

Hello is not an asset, it's a domain class. Hello不是资产,而是域类。 So you don't need to annotate it. 因此,您无需注释它。

Also make sure that you have some json support (I think having Jackson is preferred). 还要确保您有一些json支持(我认为最好是Jackson)。 By default there is no JSON provided registered. 默认情况下,没有提供注册的JSON。

I do not know what exactly you mean with 'that have a structure similar with the java class'. 我不知道您对“具有与Java类相似的结构”的确切含义。 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. 如果您要返回一个JSON结构,则可以添加以下内容:要返回JSON,我使用了: @Produces("application/json")然后将字符串解析为JSON格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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