Reputation: 202
I want to accept and respond JSON objects in a REST Application. The data I need to send and receive are in a .properties file. I have already read them and are now in a Properties
Object(From java.util.Properties
). Is there a way to marshal and unmarshal Properties
Objects, without implementing a new class?
I am using Jax-rs API in a Weblogic server.
@POST
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public JSONObject getbyID(@PathParam("id")JSONObject inputJsonObj) {
//marshalling and unmarshalling goes here
}
Upvotes: 1
Views: 4574
Reputation: 209004
Not too familiar with WebLogic, so I don't know what version of Jersey it used (1.x or 2.x), but with the 1.x you can simply add this dependency
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>${jersey-version}</version>
</dependency>
which will depend on Jackson. Jackson already deserializes and serializes the Properties
object to a JSON object.
Here's a simple test
Resource
@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();
}
}
Test
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);
}
Results:
// from get
{"prop3":"value3","prop2":"value2","prop1":"value1"}
// from post
prop3=value3
prop2=value2
prop1=value1
For the configuration, you just need to configure the POJOMapping Feature and register the Jackson provider
Programmatic
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>
With Jersey 2.x, it's a little bit simpler. We just need this provider
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.4.0</version>
</dependency>
And register the same JacksonJaxbJsonProvider
(though different package, the class name is the same). No Pojo Mapping Feature needed.
Note: In both cases, there are two Jackson providers, a JacksonJsonProvider
and a JacksonJaxbJsonProvider
. If you want the marshalling of pojos to depend on JAXB annotations, then you should register the latter.
Upvotes: 2