I have to PUT a customer object from android client to the database through Restful service (.Net)
Service Contract
[WebInvoke(Method = "PUT", UriTemplate = "customers/{customerId}", RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
[OperationContract]
ReturnValueLong PutCustomer(string customerId, Customer entity);
Customer.cs
public class Customer
{
[DataMember]
public long SystemId{ get; set; }
[DataMember]
public long CustomerId { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Description { get; set; }
}
On the client side I'm using Spring for Android to communicate.
Client Code:
Customer customer = new Customer();
customer.setCustomerId(12);
customer.setName("sample name");
customer.setDescription("sample description");
customer.setSystemId(123);
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.APPLICATION_XML);
// Prepare header
HttpHeaders headers = new HttpHeaders();
headers.setAccept(acceptableMediaTypes);
// Pass the new person and header
HttpEntity<Customer> entity = new HttpEntity<Customer>(
customer, headers);
final String url = "https://192.168.2.119:8009/IAdministratorService/customers/{customerId}";
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(
new SimpleXmlHttpConverter());
ResponseEntity<NCheckReturnValue> result = restTemplate
.exchange(url, HttpMethod.PUT, entity,
NCheckReturnValue.class, 12);
return "Return Code:" + result.getBody().getCode()
+ " Return Value: "
+ result.getBody().getReturnValue();
Customer.java
@Root(name = "Customer")
@Namespace(reference = "NCheck.Core.Model")
public class Customer {
@Element
private long SystemId;
@Element
private long CustomerId;
@Element
private String Name;
@Element
private String Description;
// Getters and Setters
}
When I execute the code the Customer object not received properly on the Server side. (Name is correct but the Description is null)
What is the wrong with this implementation?
Finally I found the solution.
Here the problem is order of elements in XML after serialization. WCF service contract expects the elements in a particular order (Even though it breaks the XML specification). But when serializing using SimpleXmlHttpConverter (I believe it use the Persistor inside) to serialize we can't guarantee the order of elements appear in the XML output.
Solution:
There is a tag available called @Order (org.simpleframework.xml.Order)
Another Solution could be do some changes in the Server Side by using [XmlSerializerFormat] tag on ServiceContracts which doesn't require any order of elements.
If you are able to put 'name', there there is no reason why not 'description'. Generally if an attribute is sent as null, it will be because the annotation is not properly defined. Possible solutions,
You may try writing something similar suitable to your purpose.
good luck!