javascript 使用 Jersey 返回字符串的 JSON 表示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12476806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Returning the JSON representation of a String with Jersey
提问by Reini
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:
我即将用 Jersey 设置一个 REST-Web 服务。目前,我对 JSON 中字符串或其他值类型的正确表示有点困惑。这里有两个片段:
@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:
在浏览器中调用此方法将显示一个“好”的 JSON 字符串,如下所示:
{"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):
在浏览器中调用该方法将显示一个没有任何 JSON-Stuff(花括号等)的纯字符串:
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.
我想使用 dojo 工具包使用此服务。问题是,一旦我设置了 [handleAs: "json"] 标志,我就会得到第二种方法的一个。它向我抛出一个错误“SyntaxError: Unexpected token H”,其中“H”是返回字符串的第一个字母。
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?
那么:字符串和其他值类型的正确 json 表示是什么,我必须为我的方法设置哪些注释来生成这些?
采纳答案by basiljames
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.
您应该定义 aDTO
并将您的String
放入其中。因此,您将拥有一个HelloResp
带有一个 String 作为属性的类。在您的方法中填充该属性并返回。
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 {
)
Firefox 没有显示错误,因为它没有处理您的响应。显示服务返回的任何内容。然而,工具包开始将响应作为 JSON 处理,但不是有效的 JSON(JSON 以 开头{
)
回答by SiMet
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.
为每个对象创建 DTO 也只是为一个 String 看起来很奇怪。
Is there any better option?
有没有更好的选择?
回答by Pau
If you are returning a String why do you define it as a type JSON?
如果您要返回一个字符串,为什么要将其定义为 JSON 类型?
Just return it as a plain text (MediaType.TEXT_PLAIN):
只需将其作为纯文本返回 (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();
}