demo类
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="demo")
public class Demo {
private String name;
@XmlElement(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
service 内容
import javax.jws.WebService;
@WebService
public interface DemoService {
String sayHello(String param);
}
serviceimpl 内容
import java.io.StringReader;
import javax.jws.WebService;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.springframework.stereotype.Service;
import com.test.service.DemoService;
import com.test.service.Demo;
@WebService(endpointInterface = "com.test.service.DemoService")//接口包路径+接口名
@Service("demoService")
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String param) {
try {
JAXBContext context = JAXBContext.newInstance(Demo.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Demo demo = (Demo)unmarshaller.unmarshal(new StringReader(param));
System.out.println(demo.getName());
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "success";
}
}
applicationContext.xml 文件配置
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<jaxws:endpoint id="demoService" implementor="com.test.service.webservice.DemoServiceImpl" address="/demoService"/>
注:implementor为实现类所在包路径+实现类名
web.xml配置url访问路径
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
访问 http://ip:port/项目名/services(web.xml中配置的url-pattern) 查看是否有发布的接口名
注:图中wsdl 中{}内为@WebService中指定的Namespace值,未指定的默认为实现类所在包的路径的倒过来的路径,若接口与实现类不在同一包路径下,则实际为接口所在包路径的倒过来的路径,由于上面未设置所以上图中即为默认namespace值
Endpoint address:结构为:url+applicationContext.xml 中配置的address
若看到上图则表示接口发布完成,接下来进行访问测试
import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
public class tes {
public static void main(String[] args) throws JAXBException {
String endpoint = "http://127.0.0.1:8080/iiacp-zj-gui/services/demoService?wsdl";// 远程请求访问wsdl文件
String params = "<?xml version=\"1.0\" encoding=\"GBK\"?><demo><name>233</name></demo>";
String returnData;
try {
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);// 远程调用路径
// 参数为new QName(namespace),调用方法名;若webservice未指定namespace,则默认为接口所在包路径的倒过来的路径
call.setOperationName(new QName("http://service.test.com/", "sayHello"));
// 设置参数名: 参数名,参数类型,参数模式,调用的方法若设置@webParam(name="xx"),则参数为xx,否则默认从arg0开始,若多个参数则arg+index递增
call.addParameter("arg0", XMLType.XSD_STRING, ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);// 设置被调用方法的返回值类型
returnData = (String) call.invoke(new Object[] { params });// 远程调用
System.out.println("result is " + returnData);
} catch (Exception e) {
System.err.println(e.toString());
}
}
}
容易出现的问题:1. Unexpected wrapper element xx found.
问题原因:namespace不一致
解决方法:call.setOperationName()方法调用中参数指定namespace,例:call.setOperationName(new QName("http://service.test.com/", "sayHello"));
2.Unmarshalling Error: 意外的元素 (uri:"", local:"xx")。所需元素为<{}arg0>
问题原因:实现方法未指定参数名
决绝方法:call.addParameter("arg0", XMLType.XSD_STRING, ParameterMode.IN); 默认从arg0开始,若多个参数则arg+index递增