慕侠8572583
2020-09-28 14:05:24浏览 2246
新建webservice接口
@WebService
// 使用@WebService标识让CXF知道使用该接口来创建WSDL
public interface HelloWorldService {
public String getName(String userName);
}
新建实现类
@WebService
//@Service
public class HelloWorldServiceImpl implements HelloWorldService {
// @Autowired
public String getName(String userName) {
return "Hello Spring!" + userName;
}
}
注入容器
ApplicationContext.xml中加入如下代码
<jaxws:endpoint
id="helloWorld"
implementor="org.app.demo.spring.service.impl.HelloWorldServiceImpl"
address="/HelloWorld" />
或者
<bean id=" helloWorldService" class=" org.app.demo.spring.service.impl.HelloWorldServiceImpl" />
<jaxws:endpoint
id="helloWorld"
implementor="#helloWorldService"
address="/HelloWorld" />
注意:XML头文件需相应添加
xmlns:jaxws="http://cxf.apache.org/jaxws
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
servlet
web.xml中加入如下代码
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>