【问题标题】:How to Convert a Properties Object to JSON Object with JAXB如何使用 JAXB 将属性对象转换为 JSON 对象
【发布时间】:2014-12-08 08:26:34
【问题描述】:

我想在 REST 应用程序中接受和响应 JSON 对象。我需要发送和接收的数据位于 .properties 文件中。我已经阅读了它们,现在位于Properties 对象中(来自java.util.Properties)。有没有办法在不实现新类的情况下编组和解组 Properties 对象?

我在 Weblogic 服务器中使用 Jax-rs API。

@POST
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public JSONObject getbyID(@PathParam("id")JSONObject inputJsonObj) {
    //marshalling and unmarshalling goes here
}

【问题讨论】:

  • 您是否正在使用特定的 REST 框架?
  • 我在 weblogic 服务器中使用 JAX-RS。默认提供者是 weblogic 中的 Jersey。
  • 为什么要通过路径发送jsonobject?为什么不按形式?
  • 数据来自客户端应用程序

标签: java json jersey jax-rs jersey-2.0


【解决方案1】:

对WebLogic不太熟悉,所以我不知道它使用的是什么版本的Jersey(1.x或2.x),但是使用1.x你可以简单地添加这个依赖

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>${jersey-version}</version>
</dependency>

这将取决于杰克逊。 Jackson 已经将 Properties 对象反序列化并序列化为 JSON 对象。

这是一个简单的测试

资源

@Path("/properties")
public class PropertiesResource {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response getProperties() throws Exception {
        FileInputStream fis = new FileInputStream("test.properties");
        Properties props = new Properties();
        props.load(fis);
        return Response.ok(props).build();
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response postProperties(Properties properties) {
        StringBuilder builder = new StringBuilder();
        for (String key: properties.stringPropertyNames()) {
            builder.append(key).append("=")
                    .append(properties.getProperty(key)).append("\n");
        }
        return Response.created(null).entity(builder.toString()).build();
    }
}

测试

public void testMyResource() throws Exception {
    ClientConfig config = new DefaultClientConfig();
    config.getClasses().add(JacksonJsonProvider.class);
    config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, 
                                                       Boolean.TRUE);

    Client c = Client.create(config);

    WebResource resource = c.resource(Main.BASE_URI).path("properties");
    String json = resource.accept("application/json").get(String.class);
    System.out.println(json);

    FileInputStream fis = new FileInputStream("test.properties");
    Properties props = new Properties();
    props.load(fis);
    String postResponse 
            = resource.type("application/json").post(String.class, props);
    System.out.println(postResponse);
}

结果:

// from get
{"prop3":"value3","prop2":"value2","prop1":"value1"}

// from post
prop3=value3
prop2=value2
prop1=value1

配置方面,只需要配置POJOMapping Feature并注册Jackson提供者

程序化

public class JerseyApplication extends ResourceConfig {
    public JerseyApplication() {
        packages(...);
        getProviderClasses().add(JacksonJsonProvider.class);
        getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
    }
}

web.xml

<init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
</init-param>

使用 Jersey 2.x,它稍微简单一些。我们只需要这个提供者

<dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-json-provider</artifactId>
    <version>2.4.0</version>
</dependency>

并注册相同的JacksonJaxbJsonProvider(虽然不同的包,类名是一样的)。无需 Pojo 映射功能。


注意:在这两种情况下,都有两个 Jackson 提供程序,一个 JacksonJsonProvider 和一个 JacksonJaxbJsonProvider。如果你希望 pojo 的编组依赖于 JAXB 注释,那么你应该注册后者。

【讨论】:

  • Weblogic 12c 支持 Jersey 2.x
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-10
  • 1970-01-01
  • 2018-11-18
相关资源
最近更新 更多