1. 之前用webservice+xml调用别人接口,用这种方式调用自己用java发布的服务可以调通
服务调用:
try {
call = (Call) service.createCall();
call.setSOAPActionURI("http://。。。.HIPMessageServer");
call.setTargetEndpointAddress(new java.net.URL(endpoint));// 远程调用路径
call.setOperationName(new QName("http://。。。", "HIPMessageServer"));// 调用的方法名
// 设置参数名:
call.addParameter("arg0", // 参数名
XMLType.XSD_STRING,// 参数类型:String
javax.xml.rpc.ParameterMode.IN);// 参数模式:'IN' or 'OUT'
call.addParameter("arg1",XMLType.XSD_STRING,// 参数类型:String
javax.xml.rpc.ParameterMode.IN);
// 设置返回值类型:
//call.setReturnType(XMLType.XSD_STRING);// 返回值类型:String
result = (String) call.invoke(new Object[]{ss});// 远程调用
java服务发布:
接口
@WebService
public interface ManagerService {
String SendManagerDataInfo(String messageId, String xmlStr);
String SendDoctorDataInfo(String messageId, String xmlStr);
String PatientRegistry(String messageId, String xmlStr);
}
实现类:
@Service("PublishService")
@WebService(endpointInterface = "com.service.PublishService")
public class PublishServiceImpl implements PublishService {
@Autowired
ManagerService managerService;
@Override
public String HIPMessageServer(String serviceNum, String xmlStr) {
String result="错误";
if(serviceNum.equals("MS003")){
result = managerService.SendManagerDataInfo(serviceNum,xmlStr);
}
else if(serviceNum.equals("MS004")){
result = managerService.SendDoctorDataInfo(serviceNum, xmlStr);
}
else if(serviceNum.equals("T0004")){
result = managerService.PatientRegistry(serviceNum, xmlStr);
}
return result;
}
}
后来发现别人使用m语言写的,一般方法掉不通,换了一种调用方式直接拼装soap消息发过去就通了
String endpoint = "wsdl路径";
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnFactory.createConnection();
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
SOAPPart part = message.getSOAPPart();
SOAPEnvelope envelope = part.getEnvelope();
SOAPBody body = envelope.getBody();
QName name = new QName("命名空间", "HIPMessageServer", "tem");
SOAPBodyElement bodyElement = body.addBodyElement(name);
String s1 = "BOE0074";//Object是用来存储方法的参数
String s2 = "<![CDATA[<Request>\n" +
" <Header>\n" +
" <SourceSystem>11</SourceSystem>\n" +
" <MessageID>11</MessageID>\n" +
" </Header>\n" +
"<Body>\n" +
" <MedExamRt>\n" +
" <PATPatientID>101100000002</PATPatientID>\n" +
" </MedExamRt>\n" +
" </Body>\n" +
"</Request>]]>";
QName input1 = new QName("http://namespace", "input1", "tem");
QName input2 = new QName("http://namespace", "input2", "tem");
bodyElement.addChildElement(input1).setValue(s1);
bodyElement.addChildElement(input2).setValue(s2);
message.writeTo(System.out);
System.out.println();
URLEndpoint destination = new URLEndpoint(endpoint);
SOAPMessage reply = connection.call(message, destination);
TransformerFactory transformerFactory =
TransformerFactory.newInstance();
Transformer transformer =
transformerFactory.newTransformer();
Source sourceContent = reply.getSOAPPart().getContent();
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
System.out.println();
connection.close();
3. 解析soap消息并处理其乱码
SOAPMessage reply = connection.call(message, destination);
org.w3c.dom.Document document1 = reply.getSOAPPart().getEnvelope().getOwnerDocument();
StringWriter output1 = new StringWriter();
TransformerFactory.newInstance().newTransformer().transform( new DOMSource(document1), new StreamResult(output1));
System.out.println("原始为"+output1);
System.out.println("字符串为"+output1.toString());
String ss = output1.toString();
String sw = ss.replace("<","<");
String sri = sw.replace(">",">");
System.out.println("转换后的字符串"+sri);
String tems1 = sri.toString().replace("<![CDATA[","");
String tempst1 = tems1.replace("]]>","");
System.out.println("字符串为"+tempst1);
StringReader sr1 = new StringReader(tempst1);
InputSource is1 = new InputSource(sr1);
DocumentBuilderFactory factory2 = DocumentBuilderFactory.newInstance();
DocumentBuilder builder1=factory2.newDocumentBuilder();
org.w3c.dom.Document doc1 = builder1.parse(is1);
System.out.println(doc1);
System.out.println("成功获取"+doc1.getElementsByTagName("MedExamID").item(0).getTextContent());