spring - 消耗SOAP Web服务错误(未注册编码(marshal)处理程序。请检查WebServiceTemplate的配置)

标签 spring web-services spring-boot soap wsdl

我遵循了入门-使用SOAP Web服务(https://spring.io/guides/gs/consuming-web-service/)来使用特定的Web服务,并且一切正常:

我做了配置类:

@Configuration
public class PMConfiguration {
    @Bean
    public Jaxb2Marshaller marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        // this package must match the package in the <generatePackage> specified in
        // pom.xml
        marshaller.setContextPath("com.inteligenciaweb.wsdl");
        return marshaller;
    }

    @Bean
    public ProcuraPMPorREClient procuraPMPorREClient(Jaxb2Marshaller marshaller) {
        ProcuraPMPorREClient client = new ProcuraPMPorREClient();
        client.setDefaultUri("http://tempuri.org/procuraPMPorRE");
        client.setMarshaller(marshaller);
        client.setUnmarshaller(marshaller);
        return client;
    } 

}

客户:
public class ProcuraPMPorREClient extends WebServiceGatewaySupport {

    private static final Logger log = LoggerFactory.getLogger(ProcuraPMPorRE.class);


    public ProcuraPMPorREResponse getPMPorRE(Integer RE) {

        ProcuraPMPorRE request = new ProcuraPMPorRE();
        request.setPMRENum(RE);

        log.info("Requesting PM for " + RE);

        ProcuraPMPorREResponse response = (ProcuraPMPorREResponse) getWebServiceTemplate()
                .marshalSendAndReceive("http://webservices.externo.policiamilitar.sp.gov.br:8071/router/wsscpm/basic",
                        request,
                        new SoapActionCallback("http://tempuri.org/procuraPMPorRE"));

        return response;
    }

}

在类里面的应用程序:
@SpringBootApplication
public class InteligenciawebApplication {

    public static void main(String[] args) {
        SpringApplication.run(InteligenciawebApplication.class, args);
    }

    @Bean
    CommandLineRunner lookup(ProcuraPMPorREClient pm) {
        return args -> {
            Integer re = 123456;        
            ProcuraPMPorREResponse response = pm.getPMPorRE(re); 
            System.err.println(response.getProcuraPMPorREResult().getNomeBancoPM());
        };
    }
}

启动应用程序时,weservice调用工作正常,因此可以在控制台上查看结果。我尝试使用相同的逻辑在其他类中调用此Web服务,但无法正常工作。例如,我已经在Controller Class上进行了测试:
@RequestMapping(value = "/soap", method = RequestMethod.GET)
public String testeSoap() {
    ProcuraPMPorREClient pm = new ProcuraPMPorREClient();
    ProcuraPMPorREResponse response = pm.getPMPorRE(123456); 
    System.out.println(response.getProcuraPMPorREResult().getNomePM());
  return null;
}

在这种情况下,Web服务将无法运行,并且系统会显示以下错误消息:java.lang.IllegalStateException:没有注册编码。检查WebServiceTemplate的配置。我不知道为什么,但是Web服务只能在特定的地方工作,而不能在其他地方工作。如果有人知道发生了什么,我将不胜感激!谢谢!

最佳答案

在这种情况下,我无法像我一样在Controller中实例化一个新对象:

ProcuraPMPorREClient pm = new ProcuraPMPorREClient();

取而代之的是,我需要创建一个@Autowired对象,如下所示:
@Autowired ProcuraPMPorREClient pm;

之后,我只调用相同的例程:
ProcuraPMPorREResponse response = pm.getPMPorRE(123456); 
System.out.println(response.getProcuraPMPorREResult().getNomePM());

这很好。

关于spring - 消耗SOAP Web服务错误(未注册编码(marshal)处理程序。请检查WebServiceTemplate的配置),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47886140/

相关文章:

java - Spring MVC 和 Apache Hadoop 启动 MapReduce Job

java - 使用自定义 xml 协议(protocol)实现客户端,无需定义或模式

java - 如何初始化 spring-boot WebMvc? (ServletContext 始终为空)

java - 无法在 Spring Boot 应用程序中访问和使用静态资源

java - 当有请求正文时模拟 WebClient 帖子

java - 来自 Maven 依赖项的 Spring Boot RestController 不起作用

javascript - 如何从 ember 调用 web 服务

Javassist 编译导致损坏的类文件

java - Spring CSS 图像加载

node.js - 是否有通用方法来开发 Amazon Echo 技能和 Google Home 操作?