Java 使用SpringRESTTemplate以XML正文作为字符串发布请求
我需要打一个rest电话在Linkedin上分享帖子,我正在使用Spring社交Linkedin模块来实现这一点。不幸的是,我不能简单地使用Java 使用SpringRESTTemplate以XML正文作为字符串发布请求,java,spring,rest,resttemplate,spring-social-linkedin,Java,Spring,Rest,Resttemplate,Spring Social Linkedin,我需要打一个rest电话在Linkedin上分享帖子,我正在使用Spring社交Linkedin模块来实现这一点。不幸的是,我不能简单地使用 org.springframework.social.linkedin.api.NetworkUpdateOperations.share(NewShare共享) 方法,我正在处理一个向用户提供原始rest模板的项目,用户可以使用它对任何url进行任何rest调用。我使用SpringSocial的Linkedin模块只是为了进行身份验证(并且它可以正常工作
org.springframework.social.linkedin.api.NetworkUpdateOperations.share(NewShare共享)
方法,我正在处理一个向用户提供原始rest模板的项目,用户可以使用它对任何url进行任何rest调用。我使用SpringSocial的Linkedin模块只是为了进行身份验证(并且它可以正常工作)
因此,我向用户提供Linkedin Spring社交Linkedin模块背后的rest模板。他们应该能够在运行时使用给定的url和数据向Linkedin发布共享。请求正文应该包含如下内容(取自):
为了共享,我使用该xml创建了一个字符串,并使用命令postForObject
。像这样:
String toShare = "<share><comment>Check out..." // the string of xml
Object result = linkedinRestTemplate.postForObject("https://api.linkedin.com/v1/people/~/shares", toShare, Object.class);
NewShare share = new NewShare();
share.set... // set its properties, sub-classes, content etc.
Object result = linkedinRestTemplate.postForObject("https://api.linkedin.com/v1/people/~/shares", newShare, Object.class);
这个电话成功了
但是我不能将我的字符串反序列化到NewShare中,因为我提供了一个API,所以我假设我完全没有关于请求主体的信息
那么,如何才能使spring正确处理字符串xml体并进行正确的服务调用呢
NewShare share = new NewShare();
share.set... // set its properties, sub-classes, content etc.
Object result = linkedinRestTemplate.postForObject("https://api.linkedin.com/v1/people/~/shares", newShare, Object.class);