Webservice

tomcat开启后 

localhost:8080/项目名/webservices/wsdemo

soapui解析地址:localhost:8080/项目名/webservices/wsdemo?wsdl

Web.xml

    <servlet>
        <display-name>CXF Servlet</display-name>
        <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>/webservices/*</url-pattern>
    </servlet-mapping>

Spring配置文件

<jaxws:endpoint id="wsdemo" address="/wsdemo" implementorClass="com.bjs.ws.Ws" implementor="com.bjs.wsi.Wsi"  />   //implementorClass接口 implementor实现类

Ws.java

package com.bjs.ws;

import java.util.Date;
import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

import com.common.dao.BaseQueryRecords;
import com.example.model.Example;

@WebService(targetNamespace = "http://webservice.com", name = "WebServiceExample") //文档说明
public interface Ws {
    @WebMethod(operationName = "add")
    @WebResult(name="result")       //无返回值不用写@WebResult
    public String add(@WebParam(name = "Name") String name,
            @WebParam(name = "Age") int age, @WebParam(name = "Born") Date born);

    @WebMethod(operationName = "delete")
    @WebResult(name="result")
    public String delete(@WebParam(name = "Id") int id);

    @WebMethod(operationName = "update")
    @WebResult(name="result")
    public String update(@WebParam(name = "Id") int id,
            @WebParam(name = "Name") String name,
            @WebParam(name = "Age") int age, @WebParam(name = "Born") Date born);
    
    @WebMethod(operationName = "find")
    @WebResult(name="result")
    public Example find(@WebParam(name = "Id") int id);
    
    @WebMethod(operationName = "findAll")
    @WebResult(name="result")
    public BaseQueryRecords findAll(@WebParam(name = "Page") int page,@WebParam(name = "Rows") int rows);
}

Wsi.java

package com.bjs.wsi;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.jws.WebService;

import org.eclipse.jetty.io.NetworkTrafficListener.Empty;

import com.bjs.ws.Ws;
import com.common.dao.BaseQueryRecords;
import com.common.dao.impl.BaseDaoDB;
import com.common.service.BaseService;
import com.example.dao.ExampleDao;
import com.example.dao.impl.ExampleDaoImpl;
import com.example.model.Example;

@WebService(endpointInterface = "com.bjs.ws.Ws")
public class Wsi extends BaseService implements Ws {

    @Resource(name = "exampledao")
    private ExampleDao exampleDao;

    @Override
    public String add(String name, int age, Date born) {
        // TODO Auto-generated method stub
        Example example = new Example(name, age, born);
        this.exampleDao.addExamples(example);
        System.out.println("add success!");
        return "add success!";
    }

    @Override
    public String delete(int id) {
        // TODO Auto-generated method stub
        Example example = this.exampleDao.findExamples(id);
        this.exampleDao.deleteExamples(example);
        System.out.println("delete success!");
        return "delete success!";
    }

    @Override
    public String update(int id, String name, int age, Date born) {
        // TODO Auto-generated method stub
        Example example = this.exampleDao.findExamples(id);
        example.setAge(age);
        if ((name!=null)&&(!"?".equals(name))) {
            example.setName(name);
        }
        if (born != null) {
            example.setBorn(born);
        }
        this.exampleDao.updateExamples(example);
        System.out.println("update success!");
        return "update success!";
    }

    @Override
    public Example find(int id) {
        // TODO Auto-generated method stub
        System.out.println("find success!");
        return this.exampleDao.findExamples(id);
    }

    @Override
    public BaseQueryRecords findAll(int page,int rows) {
        // TODO Auto-generated method stub
        System.out.println("findAll success!");
        return this.exampleDao.getExamples(page, rows);
    }

}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。