我正在使用JaxRs服务与PHP应用程序进行RESTFul通信。 我的PHP学院抱怨JSON格式。布尔值以String形式传递,而不仅仅是true或false。
{
"cusNO":"13493198",
"elmFIRSTNAME":"John",
"elmLASTNAME":"Smith",
"elmCOMPANYNAME":"Acme",
"addrSTREET":"Rock Street",
"addrNO":"1",
"cityID":"122704",
"zipcodeID":"35525",
"addrID":"12820372",
"default":"true"
}
web.xml如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>QISADS</display-name>
<servlet>
<description>JAX-RS Tools Generated - Do not modify</description>
<servlet-name>JAX-RS Servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.feature.DisableXmlSecurity</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS Servlet</servlet-name>
<url-pattern>/jaxrs/*</url-pattern>
</servlet-mapping>
</web-app>
如何告诉服务不要对某些数据类型使用引号?
我看到在JSONConfiguration中使用非字符串方法是可能的,但我看起来需要显式设置实体bean的每个字段以实现此目的。
我更喜欢s.t.这适用于所有类自动。 S.T.我可以定义所有布尔值,整数...必须是非字符串。
谢谢你的帮助
答案 0 :(得分:0)
我在网上找到了解决方案。它非常灵活,但每次在项目中添加新类时,您都需要考虑将类添加到此类的数组中。
public class MyJsonProvider implements ContextResolver<JAXBContext> {
private JAXBContext context;
private Class<?>[] types = {
MyFirstClass.class,
MySecondClass.class
};
public MyJsonProvider() throws Exception {
JSONConfiguration config = JSONConfiguration
.natural()
.humanReadableFormatting(true)
.build();
this.context = new JSONJAXBContext(config, types);
}
@Override
public JAXBContext getContext(Class<?> type) {
for (Class<?> c : types) {
if (c.equals(type))
return context;
}
return null;
}
}