我已经按照入门 - 使用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:没有marshaller注册。检查WebServiceTemplate的配置。我不知道为什么,但网络服务在特定的地方工作而在另一个地方不起作用。如果有人知道发生了什么,我会感到沮丧!谢谢!
答案 0 :(得分:5)
在这种情况下,我无法在控制器中实现一个新对象,就像我已经完成的那样:
ProcuraPMPorREClient pm = new ProcuraPMPorREClient();
而不是这个,我需要创建一个@Autowired对象,如下所示:
@Autowired ProcuraPMPorREClient pm;
之后,我只调用相同的例程:
ProcuraPMPorREResponse response = pm.getPMPorRE(123456);
System.out.println(response.getProcuraPMPorREResult().getNomePM());
这很好用。