如何在同一个Spring服务中实现多个SFDC出站消息接收器



对于每条出站消息,Salesforce提供了一个完整的自包含WSDL。

实现单个Spring服务很容易,使用jaxws-maven-plugin生成类,使用@Endpoint, @PayloadRoot等来绑定端点。

然而,对于不同的结构和类型层次结构,多个出站消息都共享相同的qn(例如http://soap.sforce.com/2005/09/outbound:notificationsurn:sobject.enterprise.soap.sforce.com:sObject)。

我知道如何将相同的XML名称映射到基于URL路径的不同处理程序。

我知道如何为绑定文件生成的类使用单独的包:

<?xml version="1.0" encoding="UTF-8"?>
<jaxws:bindings
    xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    wsdlLocation="../wsdl/SFDC_Contact_Outbound_Msg.wsdl"
    version="2.0">
  <jaxb:bindings node="//xs:schema[@targetNamespace='http://soap.sforce.com/2005/09/outbound']">
    <jaxb:schemaBindings>
      <jaxb:package name="com.sforce.soap.outbound.contact"/>
    </jaxb:schemaBindings>
  </jaxb:bindings>
  <jaxb:bindings node="//xs:schema[@targetNamespace='urn:sobject.enterprise.soap.sforce.com']">
    <jaxb:schemaBindings>
      <jaxb:package name="com.sforce.soap.enterprise.sobject.contact"/>
    </jaxb:schemaBindings>
  </jaxb:bindings>
</jaxws:bindings>

然而,当尝试从生成的代码初始化Jaxb2Marshaller时,它仍然无法处理XML冲突:

[WARN] [main] 09:40:45.687 AnnotationConfigEmbeddedWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException:
  Error creating bean with name 'marshaller' defined in class path resource [WebServiceConfig.class]: 
  Invocation of init method failed; nested exception is org.springframework.oxm.UncategorizedMappingException:
  Unknown JAXB exception; nested exception is com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 6 counts of IllegalAnnotationExceptions
  Two classes have the same XML type name "{urn:sobject.enterprise.soap.sforce.com}sObject". Use @XmlType.name and @XmlType.namespace to assign different names to them.
  ...

当sdfc生成的WSDL发生变化时,我不想再添加任何手动步骤,除了添加新文件。

是否有一种方法可以在不更改源WSDL的情况下更改package-info.java中的名称空间?

是否有一种方法可以轻松地(即不使用每个单独的@Bean方法)为每个包创建一个单独的编组器,然后将其添加到DefaultMethodEndpointAdapter中?

是否有另一种方法来实现所有这些出站消息接收器?

是否有改变命名空间的方法?

如果您这样做,它们将不匹配实际消息中的名称空间,因此它将无法反编组。

是否有一种方法可以轻松地为每个包创建单独的编组器?

好吧,这里有一个的方法。

@PostConstruct
public void marshallers() throws IOException {
  List<String> types = Arrays.stream(applicationContext.getResources("classpath:com/sforce/soap/outbound/*"))
      .map(Resource::getFilename)
      .collect(Collectors.toList());
  for (String type : types) {
    String beanName = "marshallingPayloadMethodProcessor_" + type;
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setPackagesToScan(
        "com.sforce.soap.outbound." + type,
        "com.sforce.soap.enterprise.sobject." + type
    );
    try {
      marshaller.afterPropertiesSet();
    } catch (Exception ex) {
      throw new BeanInitializationException("Could not initialize bean " + beanName, ex);
    }
    MarshallingPayloadMethodProcessor processor = new MarshallingPayloadMethodProcessor(marshaller);
    beanFactory.registerSingleton(beanName, processor);
  }

关于这个的几个注意事项:

  • 如果通过jar部署,则类路径目录不存在,因此需要另一种方式来获取包名。
  • registerSingleton似乎打破了一些应用程序上下文-导致不相关的bean不再被发现。我不知道这是为什么。

相关内容

最新更新