2

我需要打个休息电话以在 Linkedin 中分享帖子,为此我正在使用Spring Social Linkedin模块。不幸的是我不能简单地使用

org.springframework.social.linkedin.api.NetworkUpdateOperations.share(NewShare share)

方法,我正在开发一个向用户提供原始休息模板的项目,用户可以使用它对任何 url 进行任何休息调用。我仅将 Spring Social 的 Linkedin 模块用于身份验证部分(它应该可以正常工作)。

因此,我向用户提供了 Linkedin Spring Social Linkedin 模块背后的其余模板。他们应该能够在运行时使用给定的 url 和数据将共享发布到 Linkedin。请求正文应包含以下内容(取自此处):

<share>
  <comment>Check out the LinkedIn Share API!</comment>
  <content>
    <title>LinkedIn Developers Documentation On Using the Share API</title>
    <description>Leverage the Share API to maximize engagement on user-generated content on LinkedIn</description>
    <submitted-url>https://developer.linkedin.com/documents/share-api</submitted-url>
    <submitted-image-url>http://m3.licdn.com/media/p/3/000/124/1a6/089a29a.png</submitted-image-url> 
  </content>
  <visibility> 
    <code>anyone</code> 
  </visibility>
</share>

为了分享,我使用该 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);

但是此调用失败并返回400 Bad Request。似乎休息模板处理这个 xml 字符串而不是作为对象,因此它没有被序列化并正确放入请求正文。

Spring Social Linkedin 做同样的事情,但只有一点不同:它有一个可序列化的类,称为NewShare它具有与该 xml 相同的结构。但是,当 NewShare 的实例作为请求主体提供时,它是成功的。像这样:

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

此调用成功。

但我无法将我的 String 反序列化为 NewShare,因为我提供了一个 API,所以我假设我完全没有关于请求正文的信息。

那么我怎样才能让spring正确处理字符串xml主体并进行正确的服务调用呢?

4

0 回答 0