使用弹簧RestTemplate从Android的PUT请求 [英] PUT request from Android using spring RestTemplate

查看:399
本文介绍了使用弹簧RestTemplate从Android的PUT请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要通过RESTful服务从Android客户端PUT客户对象数据库(.NET)

I have to PUT a customer object from android client to the database through Restful service (.Net)

服务合同

[WebInvoke(Method = "PUT", UriTemplate = "customers/{customerId}", RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
    [OperationContract]
    ReturnValueLong PutCustomer(string customerId, Customer entity);

Customer.cs

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; }
}

在客户端,我使用Spring Android的沟通。

On the client side I'm using Spring for Android to communicate.

客户端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

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


}

当我执行code中的客户对象不是在服务器端正确接收。 (名称是正确的,但描述为空)

When I execute the code the Customer object not received properly on the Server side. (Name is correct but the Description is null)

什么是不对的实现?

推荐答案

最后,我找到了解决办法。

Finally I found the solution.

下面的问题是为了序列化后的XML元素。 WCF服务合同期望以特定的顺序(即使它打破XML规范)的元素。但在使用SimpleXmlHttpConverter序列化时,(我相信它使用Persistor内)序列化,我们不能保证顺序的元素出现在XML输出​​。

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.

解决方法:

有可用的一个标签叫@Order(org.simpleframework.xml.Order)

There is a tag available called @Order (org.simpleframework.xml.Order)

@Root(name = "Customer")
@Namespace(reference = "NCheck.Core.Model")
@Order(elements = {"CustomerId", "Description", "Name" })
public class Customer {

@Element
private long CustomerId;

@Element
private String Name;

@Element
private String Description;

public long getCustomerId() {
    return CustomerId;
}
public void setCustomerId(long customerId) {
    CustomerId = customerId;
}
public String getName() {
    return Name;
}
public void setName(String name) {
    Name = name;
}
public String getDescription() {
    return Description;
}
public void setDescription(String description) {
    Description = description;
}
}

另一个解决方法可以是通过使用[XmlSerializerFormat]标签上ServiceContracts其不需要元件的任何顺序执行在服务器端的一些变化。

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.

这篇关于使用弹簧RestTemplate从Android的PUT请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆