使用 Apache cxf 创建 WebService 服务端

ws-server-demo

本地服务地址(具体端口跟tomcat配置有关)

http://localhost:8280/services/HelloWorld?wsdl

spring + cfx 配置

maven

添加依赖包


  org.apache.cxf
  cxf-rt-frontend-jaxws
  3.2.3


  org.apache.cxf
  cxf-rt-transports-http
  3.2.3

web.xml

在web.xml 增加 spring 的配置和 CXFServlet 的配置

    
        contextConfigLocation
        classpath:applicationContext.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    
    
        CXFServlet
        org.apache.cxf.transport.servlet.CXFServlet  
        1
    
    
        CXFServlet  
        /services/* 
    

编写服务

package com.example.demo.ws.service;

import javax.jws.WebService;
import java.util.Date;


@WebService
public interface HelloWorld {

    String sayHi(String text);

    Date currentDate();
}
package com.example.demo.ws.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jws.WebService;
import java.util.Date;

@WebService(endpointInterface = "com.example.demo.ws.service.HelloWorld")
public class HelloWorldImpl implements HelloWorld {

    private Logger logger = LoggerFactory.getLogger(getClass());

    public String sayHi(String text) {
        logger.info("sayHi called");
        return "Hello " + text;
    }

    @Override
    public Date currentDate() {
        logger.info("currentDate called");
        return new Date();
    }
}

applicationContext.xml

在 applicationContext.xml 增加 Service 的配置,记得要 import cxf 的配置




    
    
    

WSDL

http://localhost:8280/services/HelloWorld?wsdl

就能看到 XML 接口说明

可以直接运行的demo

GitHub https://github.com/openmartin...

你可能感兴趣的:(java,web-service,cxf)