How to pass user defined objects with a CXF REST client?
I'm developing an android application that needs to connect to a remote database through a CXF web service.I have tried using Soap but due to various problems left the option and went for a light weight REST based service(by adding the annotations to the existing cxf web service).I have a Rest client which is 开发者_开发百科called from inside the activity. I worked with simple parameters like String,int etc.Now i Want to pass a user Defined object to the service and get some String value from the server side.How do i do this? Please help...On googling i found articles about using JSON,JAXB etc but I don know what these do or how i use these either. I am very new to programming using these technologies.
You could do something similar for your client code:
private static final String URI = "http://localhost/rest/customer";
private Customer readCustomer(String id) {
try {
URL url = new URL(URI + "/" + id);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/xml");
InputStream data = connection.getInputStream();
// TODO - Read data from InputStream
connection.disconnect();
return customer;
} catch(Exception e) {
throw new RuntimeException(e);
}
}
private void createCustomer(Customer customer) {
try {
URL url = new URL(URI);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/xml");
OutputStream os = connection.getOutputStream();
// TODO - Write data to OutputStream
os.flush();
connection.getResponseCode();
connection.disconnect();
} catch(Exception e) {
throw new RuntimeException(e);
}
}
private void updateCustomer(Customer customer) {
try {
URL url = new URL(URI);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("PUT");
connection.setRequestProperty("Content-Type", "application/xml");
OutputStream os = connection.getOutputStream();
// TODO - Write data to OutputStream
os.flush();
connection.getResponseCode();
connection.disconnect();
} catch(Exception e) {
throw new RuntimeException(e);
}
}
private void deleteCustomer(String id) {
try {
URL url = new URL(URI + "/" + id);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("DELETE");
connection.getResponseCode();
connection.disconnect();
} catch(Exception e) {
throw new RuntimeException(e);
}
}
精彩评论