我必须通过 Restful 服务(.Net)将客户对象从 Android 客户端放入数据库
服务合同
[WebInvoke(Method = "PUT", UriTemplate = "customers/{customerId}", RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
[OperationContract]
ReturnValueLong PutCustomer(string customerId, Customer entity);
客户.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; }
}
在客户端,我使用 Spring for Android 进行通信。
客户端代码:
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();
客户.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
}
当我执行代码时,服务器端未正确接收客户对象。
(名称正确,但描述为空)
这个实现有什么问题吗?