我正在构建一个RESTful Web服务。我被锁定在无法继续的情况下。我有一个DAO(POJO),JSONObject其成员变量为。当我尝试从客户端(邮递员或用户定义的javascript)进行POST调用并尝试进行调试时,在的getter中收集的JSONObject值为空({}),而该类的其他成员则获取了它们的相应值。我尝试用来注释the JSONObject及其吸气剂@XmlElement,@JsonProperty以此类推。
JSONObject
@XmlElement
@JsonProperty
该类如下所示:
package org.somepackage import javax.xml.bind.annotation.XmlRootElement; import org.codehaus.jackson.annotate.JsonAutoDetect; import org.codehaus.jackson.annotate.JsonProperty; import org.json.JSONObject; @XmlRootElement public class someClass { private String someID; private String someName; private JSONObject someJsonObject; public someClass () { } public someClass (String id, String name, JSONObject jsonObj) { someID=id; someName=name; someJsonObject=jsonObj; } public String getSomeID() { return someID; } public void setSomeID(String id) { this.SomeID= id; } public String getSomeName() { return someName; } public void setSomeName(String name) { this.someName= name; } public JSONObject getSomeJsonObject() { return someJsonObject; } public void setSomeJsonObject(JSONObject jsonObj) { this.someJsonObject= jsonObj; } }
我感谢您的帮助!谢谢。
编辑
JSON范例
{ "name": "ABCD", "ID": "P63784433", "theJSON":{ "string":"foo", "number":5, "array":[1,2,3], "object":{ "property":"value", "subobj":{ "arr":["foo","ha"], "numero":1 } } } }
依赖 Jackson的web.xml依赖
<dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.2</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.2</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-jaxrs</artifactId> <version>1.9.2</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-xc</artifactId> <version>1.9.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.6.2</version> </dependency>
通过web.xml进行资源和供应商注册
<!-- Register JAX-RS Application --> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>my.package.MyApplication</param-value> </init-param> <!-- Register resources and providers under my.package. --> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>my.package</param-value> </init-param> <!-- Register custom provider --> <init-param> <param-name>jersey.config.server.provider.classnames</param-name> <param-value>my.package.mapper.ObjectMapperProvider</param-value> </init-param>`
MyApplication.java
`@ApplicationPath("/") public class MyApplication extends ResourceConfig { public MyApplication() { // Register resources and providers using package-scanning. packages("my.package"); register(ObjectMapperProvider.class); }`
问题是杰克逊不知道如何创建JSONObject(至少在没有帮助的情况下)。Jackson主要处理基本类型和POJO。如果您希望能够处理JSONObject(假设这是来自的对象org.json),则可jackson- datatype-json-org以为Jackson支持添加。
org.json
jackson- datatype-json-org
下面是一个完整的测试。这是我用来测试的依赖项
<dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20141113</version> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>2.16</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-json-org</artifactId> <version>2.3.2</version> </dependency> <dependency> <groupId>org.glassfish.jersey.test-framework.providers</groupId> <artifactId>jersey-test-framework-provider-grizzly2</artifactId> <version>2.16</version> <scope>test</scope> </dependency>
注意: 我使用jackson-datatype-json-org的Jackson版本与jersey-media-json- jackson2.16 使用的Jackson版本相同。如果您使用的球衣杰克逊版本不同,则需要确保引入的杰克逊版本与jackson-datatype- json-org您使用的版本相同。这样,我们就不会混合Jackson版本。
jackson-datatype-json-org
jersey-media-json- jackson
jackson-datatype- json-org
这是使用Jersey测试框架的测试
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule; import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.ext.Provider; import javax.ws.rs.ext.ContextResolver; import javax.ws.rs.client.Entity; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.glassfish.jersey.client.ClientConfig; import org.glassfish.jersey.jackson.JacksonFeature; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.test.JerseyTest; import org.json.JSONObject; import org.junit.Test; import static junit.framework.Assert.*; /** * * @author Paul Samsotha */ public class JsonOrgTest extends JerseyTest { public static class Model { public String firstName; public String lastName; public JSONObject other; // should br private with correct getters and setters } @Path("model") public static class ModelResource { @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response post(Model model) { return Response.ok(model).build(); } } @Provider public static class ObjectMapperProvider implements ContextResolver<ObjectMapper> { private final ObjectMapper mapper; public ObjectMapperProvider() { mapper = new ObjectMapper(); mapper.registerModule(new JsonOrgModule()); } @Override public ObjectMapper getContext(Class<?> type) { return mapper; } } @Override public ResourceConfig configure() { return new ResourceConfig(ModelResource.class) .register(ObjectMapperProvider.class) .register(JacksonFeature.class); } @Override public void configureClient(ClientConfig config) { config.register(JacksonFeature.class); config.register(ObjectMapperProvider.class); } @Test public void should_return_org_json_data() { final String json = "{\n" + " \"firstName\": \"pee\",\n" + " \"lastName\": \"skillet\",\n" + " \"other\": {\n" + " \"age\": 100,\n" + " \"birthday\": \"yesterday\"\n" + " }\n" + "}"; Response response = target("model").request().post(Entity.json(json)); if (response.getStatus() != 200) { System.out.println(response.getStatus() + ": " + response.readEntity(String.class)); fail("should return data and 200"); } else { Model model = response.readEntity(Model.class); JSONObject other = model.other; System.out.println(other.toString()); assertEquals("pee", model.firstName); assertEquals("skillet", model.lastName); assertEquals(100, other.getInt("age")); assertEquals("yesterday", other.getString("birthday")); } } }
您还应该做的是摆脱上面评论中的 所有 Jackson依赖项。您只需要一个依赖项即可支持Jackson JSON。
<dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>2.16</version> </dependency>
还要注意ObjectMapperProvider测试中的。您将需要此注册JsonOrgModule与ObjectMapper为了让杰克逊能够处理JSONObject。这个很重要。如果您没有ContextResolver,则上面的示例将失败。
ObjectMapperProvider
JsonOrgModule
ObjectMapper
ContextResolver