当前位置 : 主页 > 网页制作 > Nodejs >

CXF Webservice nonSpring

来源:互联网 收集:自由互联 发布时间:2021-06-24
不使用Spring的情况下创建CXF Webservice。 需要通过继承CXFNonSpringServlet来发布service。 需要的jar asm-3.3.1.jar asm-attrs-2.2.3.jar cxf-api-2.7.8.jar cxf-rt-bindings-soap-2.7.8.jar cxf-rt-bindings-xml-2.7.8.jar cxf-rt

不使用Spring的情况下创建CXF Webservice。

需要通过继承CXFNonSpringServlet来发布service。
  • 需要的jar
    asm-3.3.1.jar
    asm-attrs-2.2.3.jar
    cxf-api-2.7.8.jar
    cxf-rt-bindings-soap-2.7.8.jar
    cxf-rt-bindings-xml-2.7.8.jar
    cxf-rt-core-2.7.8.jar
    cxf-rt-databinding-jaxb-2.7.8.jar
    cxf-rt-frontend-jaxws-2.7.8.jar
    cxf-rt-frontend-simple-2.7.8.jar
    cxf-rt-transports-http-2.7.8.jar
    cxf-rt-ws-addr-2.7.8.jar
    cxf-rt-ws-policy-2.7.8.jar
    cxf-rt-ws-security-2.7.8.jar
    neethi-3.0.2.jar
    wsdl4j-1.6.3.jar
    wss4j-1.6.13.jar
    xml-apis-1.0.b2.jar
    xml-resolver-1.2.jar
    xmlbeans-2.6.0.jar
    xmlParserAPIs-2.0.2.jar
    xmlpull-1.1.3.1.jar
    xmlschema-core-2.0.3.jar
    xmlsec-1.5.6.jar
    xmltooling-1.3.2-1.jar
    stax2-api-3.1.1.jar
    woodstox-core-asl-4.2.0.jar

  • 代码结构
    wsclient - 客户端
    wscommon - Service接口定义
    wsservernonspring - 服务器端

  • service接口定义 - wscommon

package com.ben.service;

import javax.jws.WebService;
import com.ben.soapbean.User;

@WebService
public interface LoginService {
    public void sayLogin(User user);
}
  • Service实现 - wsservernonspring
package com.ben.serviceimpl;

import javax.jws.WebService;
import com.ben.service.LoginService;
import com.ben.soapbean.User;

@WebService(endpointInterface="com.ben.service.LoginService", serviceName="LoginService")
public class LoginServiceImpl implements LoginService{

    public void sayLogin(User user) {
        System.out.println("user login....");
    }
}
  • Service发布 - wsservernonspring
package com.ben.servlet;


import javax.servlet.ServletConfig;
import javax.servlet.annotation.WebServlet;
import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
//import org.apache.cxf.frontend.ServerFactoryBean;
import org.apache.cxf.transport.servlet.CXFNonSpringServlet;
import com.ben.serviceimpl.LoginServiceImpl;

/** * Servlet implementation class ServicePublisher */
@WebServlet
public class ServicePublisher extends CXFNonSpringServlet {
    private static final long serialVersionUID = 1L;

    /** * @see HttpServlet#HttpServlet() */
    public ServicePublisher() {
        super();
    }

    @Override
    protected void loadBus(ServletConfig sc) {
        super.loadBus(sc);
        // You could add the endpoint publish codes here
        Bus bus = this.getBus();
        BusFactory.setDefaultBus(bus);
        Endpoint.publish("/loginService", new LoginServiceImpl());
        // You can als use the simple frontend API to do this
// ServerFactoryBean factroy = new ServerFactoryBean();
// factroy.setBus(bus);
// factroy.setServiceClass(LoginServiceImpl.class);
// factroy.setAddress("/loginService");
// factroy.create();
    }
}
  • web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>wsservernonspring</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>ServicePublisher</servlet-name>
    <servlet-class>com.ben.servlet.ServicePublisher</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServicePublisher</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

Note.
*在没有配置CXF Secure的情况下,需要打开allowInsecureParser。
方法是在服务器启动命令中加入jvm参数*
-Dorg.apache.cxf.stax.allowInsecureParser=1

- 客户端调用

package com.ben.client;

import com.ben.service.LoginService;
import com.ben.soapbean.User;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class Login {
    public static void main(String[] args) {
        User user = new User();
        user.name = "tester";
        URL wsdlURL = null;
        try {
            wsdlURL = new URL("http://localhost:8080/wsservernonspring/loginService?wsdl");
            Service service = Service.create(wsdlURL, new QName("http://serviceimpl.ben.com/", "LoginService"));
            LoginService lclient = service.getPort(LoginService.class);
            lclient.sayLogin(user);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
}
网友评论