2014-12-08 195 views
1

我想接受并响应REST应用程序中的JSON对象。我需要发送和接收的数据位于.properties文件中。我已经阅读过它们,现在处于Properties对象(从java.util.Properties)。有没有一种方法来编组和解组Properties对象,而不实施一个新的类?如何使用JAXB将属性对象转换为JSON对象

我在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 
} 
+0

是否有您正在使用的特定REST框架? – 2014-12-08 08:52:21

+0

我在weblogic服务器中使用JAX-RS。默认提供者是weblogic中的Jersey。 – Shenal 2014-12-08 08:54:56

+0

你为什么通过路径发送jsonobject?为什么不通过形式? – 2014-12-08 09:08:22

回答

2

不太熟悉的WebLogic,所以我不知道是什么版本的泽西它使用(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 

对于配置,你只需要配置POJOMappin摹功能并注册杰克逊提供商

纲领性

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> 

随着新泽西2.X,这是一个有点简单。我们只需要此提供

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

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


注:在这两种情况下,有两种杰克逊提供商,一个JacksonJsonProviderJacksonJaxbJsonProvider。如果您希望pojos的编组依赖于JAXB注释,那么您应该注册后者。

+1

在weblogic 12c中支持Jersey 2.x – Shenal 2014-12-08 10:02:56