...
Code Block |
---|
language | java |
---|
title | Sample Code invoking a POST - add a user |
---|
linenumbers | true |
---|
|
User us = new User("David", "");
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(
JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
Client client = Client.create(clientConfig);
WebResource webResource = client
.resource("http://localhost:8084/dbwstier/dbrest/user/cu");
ClientResponse response = webResource.accept("application/json")
.type("application/json").post(ClientResponse.class, us);
if (response.getStatus() != 201)
{
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
String output = response.getEntity(String.class);
System.out.println("Server response .... \n");
System.out.println(output); |
To update a record we are going to use HTTP PUT command
Code Block |
---|
language | java |
---|
firstline | 26 |
---|
title | Sample Code invoking a PUT - update a user |
---|
linenumbers | true |
---|
|
User oldUser = new User("David", "ppp");
User newUser = new User("David", "PP-XX-YY");
UserMap umap = new UserMap( oldUser, newUser );
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(
JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
Client client = Client.create(clientConfig);
WebResource webResource = client
.resource("http://localhost:8084/dbwstier/dbrest/user/uu");
ClientResponse response = webResource.accept("application/json")
.type("application/json").put(ClientResponse.class, umap);
if (response.getStatus() != 200)
{
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
String output = response.getEntity(String.class);
System.out.println("Server response .... \n");
System.out.println(output);
|
Notice the new class UserMap
Code Block |
---|
language | java |
---|
title | Code Sample - UserMap |
---|
|
public class UserMap
{
public User oldUser;
public User newUser;
public UserMap(){}
public UserMap( User oldU, User newU )
{
oldUser = oldU;
newUser = newU;
}
} |
This will be a carrier object, holding two objects of type User