昨天发布了用Springboot连接webservice的帖子,但springboot仅为测试连接环境,真实项目环境是SSM,因此又对SSM连接webservice做了一些研究。
首先webservice服务端会提供一个ip给我们客户端(在这里我调第三方服务接口,我充当客户端),连接上他们的内网后进这个ip地址可以看到一堆的xml文件,在这里我用了本地获取webservice的方式。
第一步先把xml文件用cxf转为Java文件导入本地,不知道的小伙伴们可以去别的帖子搜索一下,具体很简单。
其次我使用的是soap发
@Autowired
private QNAME SERVICE QNAME SERVICE;
因此我在这里先注入了这个核心类,部分隐私信息我已做了处理,其中@Component注解是我随后加上去的,因为后续我们涉及到需要注入这个类,类的大概内容如下:
@WebServiceClient(name = "CoreToIntervention",
wsdlLocation = "第三方提供的ip地址",
targetNamespace = "targetNamespace ")
@Component
public class CoreToIntervention extends Service {
public final static URL WSDL_LOCATION;
public final static QName SERVICE = new QName("targetNamespace ", "这里是什么就autowird什么");
public final static QName CoreToInterventionHttpGet = new QName("targetNamespace ", "CoreToInterventionHttpGet");
public final static QName CoreToInterventionHttpPost = new QName("targetNamespace ", "CoreToInterventionHttpPost");
public final static QName CoreToInterventionSoap = new QName("targetNamespace ", "CoreToInterventionSoap");
public final static QName CoreToInterventionSoap12 = new QName("targetNamespace ", "CoreToInterventionSoap12");
static {
URL url = null;
try {
url = new URL("第三方提供的ip地址");
} catch (MalformedURLException e) {
java.util.logging.Logger.getLogger(CoreToIntervention.class.getName())
.log(java.util.logging.Level.INFO,
"Can not initialize the default wsdl from {0}", "第三方提供的ip地址");
}
WSDL_LOCATION = url;
}
public CoreToIntervention(URL wsdlLocation) {
super(wsdlLocation, SERVICE);
}
public CoreToIntervention(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public CoreToIntervention() {
super(WSDL_LOCATION, SERVICE);
}
public CoreToIntervention(WebServiceFeature ... features) {
super(WSDL_LOCATION, SERVICE, features);
}
public CoreToIntervention(URL wsdlLocation, WebServiceFeature ... features) {
super(wsdlLocation, SERVICE, features);
}
public CoreToIntervention(URL wsdlLocation, QName serviceName, WebServiceFeature ... features) {
super(wsdlLocation, serviceName, features);
}
现在咱们再看到这里:
CoreToInterventionSoap core = coreToIntervention.getCoreToInterventionSoap();
//解密后的数据
String dEncrypt = core.dEncrypt(core.方法名(core.encrypt(JSONObject.toJSONString(requestVO))));
logger.info("请求返回的数据:dEncrypt={}", dEncrypt);
首先先拿这个coreToIntervention去获取他的getxxxSoap方法(在这里我用soap是这样的,它同时还提供了get和post方法,根据自己的需求去选择不同的方法就可以了),在方法中已经帮你声明好了ip以及targetNamespace。
获取到了这个方法之后拿到了返回值core,我这里因为涉及到了加解密所以不对我的代码进行详细解释,解释一个大概的思想:
首先我们需要用core去调出你这里业务逻辑所需要调用的方法,然后按照他的参数类型传进去就可以了,我这里是把参数都放在了json里,但方法的参数类型是String,所以就用了FastJSON中的toJSONString ,把这个json转成String就好了,拿到这个数据后就可以做接下来的业务逻辑了。
版权声明:本文为m0_53387138原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。