Build webService Based on CXF and webService Based on CXF
1. Import the relevant jar packages. I cannot remember which packages are too clear.
2. Add related configuration information to applicationContext as follows:
<Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: xsd = "http://www.w3.org/2001/XMLSchema" xmlns: context = "http://www.springframework.org/schema/context" xmlns: jaxws = "http://cxf.apache.org/jaxws" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <context: component-scan base-package = "com. cxf. bo "/> <import resource =" classpath: META-INF/cxf. xml "/> <import resource =" classpath: META-INF/cxf/cxf-extension-soap.xml "/> <import resource =" classpath: META-INF/cxf/cxf-servlet.xml "/> <jaxws: endpoint id = "OrderWS" implementor = "com. cxf. spring. ws. orderWSImpl "// address of the class or # + id address of the corresponding bean ="/OrderWS "> // enter the name <jaxws: features> <bean class =" org. apache. cxf. feature. loggingFeature "/> </jaxws: features> </jaxws: endpoint> </beans>
3. Add the following content to the web. xml file:
<! -- Cxf configuration --> <servlet-name> CXFServlet </servlet-name> <servlet-class> org. apache. cxf. transport. servlet. CXFServlet </servlet-class> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-name> CXFServlet </ servlet-name> <url-pattern>/CXFServlet/* </url-pattern> </servlet-mapping>
4. Add the following to the service layer:
@WebServicepublic interface OrderWS { @WebMethod public Order getOrderById(int id);}
5.If struts2 exists, a conflict occurs., Then rewrite the filter.
5.1 write a class ExtendStrutsFilter:
Package com. nbu. retailer. filter; import java. io. IOException; import javax. servlet. filterChain; import javax. servlet. servletException; import javax. servlet. servletRequest; import javax. servlet. servletResponse; import javax. servlet. http. httpServletRequest; import org. slf4j. logger; import org. slf4j. loggerFactory; import org. apache. struts2.dispatcher. ng. filter. strutsPrepareAndExecuteFilter; public class ExtendStrutsFilter extends StrutsPrepareAndExecuteFilter {private static Logger log = LoggerFactory. getLogger (ExtendStrutsFilter. class); @ Override public void doFilter (ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {try {HttpServletRequest request = (HttpServletRequest) req; // determine whether the request is sent to WebService if (request. getRequestURI (). contains ("/CXFServlet") {// if it is from the request chain sent to CXFService. doFilter (req, res);} else {// default action request super. doFilter (req, res, chain) ;}} catch (Exception e) {log. error (e. getMessage (); e. printStackTrace ();}}}
5.2 change the filter address in web. xml:
<! -- Struts2 filter --> <filter-name> struts2 </filter-name> <! -- <Filter-class> org. apache. struts2.dispatcher. ng. filter. StrutsPrepareAndExecuteFilter </filter-class> --> <! -- The Custom struts2 transmitter class is used to solve the problem that struts2 treats cxf requests as actions --> <filter-class> com. nbu. retailer. filter. extendStrutsFilter </filter-class> </filter>
5.3 note,The url of CXF and struts2 cannot be the same. I have discovered this problem for a long time.