java JAX-RS REST 服务将布尔值作为字符串返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14842349/
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
JAX-RS REST service returning boolean as string
提问by Schlameel
I am using JAX-RS to produce a RESTful service. However when requesting JSON, boolean values are returned as a quoted string {"boolValue":"true"}
rather than a boolean value {"boolValue":true}
.
我正在使用 JAX-RS 来生成 RESTful 服务。但是,在请求 JSON 时,布尔值作为带引号的字符串{"boolValue":"true"}
而不是布尔值返回{"boolValue":true}
。
A simple object
一个简单的对象
@XmlRootElement
public class JaxBoolTest {
private boolean working;
public boolean isWorking() {
return working;
}
public void setWorking(boolean working) {
this.working = working;
}
}
A simple JAX-RS REST service
一个简单的 JAX-RS REST 服务
@Path("/jaxBoolTest")
public class JaxBoolTestResouce {
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public JaxBoolTest getJaxBoolTest() {
JaxBoolTest jbt = new JaxBoolTest();
jbt.setWorking(false);
return jbt;
}
}
And the result:
结果:
{"working":"false"}
How do I get the boolean values as boolean values rather than strings?
如何将布尔值作为布尔值而不是字符串获取?
回答by Kyle
Using jaxson (http://Hymanson.codehaus.org/) to serialize this worked out of the box:
使用 jaxson ( http://Hymanson.codehaus.org/) 来序列化这个开箱即用的工作:
public class BooleanTest {
@Test
public void test() throws Exception{
System.out.println(new ObjectMapper().writeValueAsString(new JaxBoolTest()));
}
}
Produced this output:
产生了这个输出:
{"working":false}
I highly recommend using Hymanson for serializing JSON. It works great. I've used Jettison in the past, but had lots of issues with it. You will probably have to configure your jax-rs provider to use Hymanson, since it doesn't look like it's already using it.
我强烈推荐使用 Hymanson 来序列化 JSON。它工作得很好。我过去曾使用过 Jettison,但遇到了很多问题。您可能必须配置您的 jax-rs 提供程序以使用 Hymanson,因为它看起来不像已经在使用它。
Another tip: no need for @XmlRootElement when using Hymanson, unless you also want to provide jax-b xml using the same beans.
另一个提示:使用 Hymanson 时不需要 @XmlRootElement,除非您还想使用相同的 bean 提供 jax-b xml。
回答by Paulino III
In my case the JSON output was displaying all properties as Strings (including primitive data types). Adding this to my web.xml fixed the problem.
在我的例子中,JSON 输出将所有属性显示为字符串(包括原始数据类型)。将此添加到我的 web.xml 解决了该问题。
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
回答by Musa
@POST @Path("/users")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Object controlUserExisting(Object requestEntity) {
boolean result=RootBsn.controlUserExisting(requestEntity);
JSONObject json=new JSONObject();
json.put("result",result);
return json.toString();
}
retrieving user existing Sended and returned{"result":true} (if exist)
检索用户现有发送并返回{"result":true}(如果存在)