Java编写一个WebService并在Tomcat上发布

Java编写一个WebService并在Tomcat上发布

本例采用Myeclipse 2016 CI 6,JDK1.8 。新建一个Web Service Project。

                                

 

 选择如下设置:

                                     

 

在src目录下建个包:

                                            

 

编写一个接口,其中一个方法返回ArrayList,另一个方法返回JSON:

package Services;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface ResultService<E> {
    public ArrayList<E> getResultInfo() throws SQLException;
    @WebMethod
    public String getJsonResultInfo();  
}

编写四个JavaBean实体类用于返回泛型ArrayList<E>类型: 

package Services;

public class Employee {     
    private String WORKCODE;
    private String LASTNAME;
    private Long SUBCOMPANYID1;
    private Long DEPARTMENTID1;
    private String JOBTITLEMARK;
    private String JOBCALL;
    private String JOBLEVEL;
    private long MANAGERID;
    private String STATUS;
    private String LOCATIONNAME;
    private String MOBILE;
    private String MOBILECALL;
    private String TELEPHONE;
    private String STARTDATE;
    private String LASTMODDATE;

    //set and get.......    
}
package Services;

public class Company {
    private long ID;
    private String SUBCOMPANYNAME;

    //get and set.......
}

 

package Services;

public class Dept {

    private long ID;
    private String DEPARTMENTNAME;
    private long SUPDEPID;
    private long SUBCOMPANYID1;
    private String DEPARTMENTMARK;
    private String CANCELED;

    //get and set......
}
package Services;

public class DeptLeader {
    private long BMFZR;
    private long BM;

    //get and set....
}

 

编写四个子类实现上面这个接口,这里就只举例一个子类吧,其他三个模式一样:

package Services;

import java.sql.SQLException;
import java.util.ArrayList;
import javax.jws.WebService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

@WebService
public class EmployeeServiceImpl implements ResultService<Employee>{

    private ArrayList<Employee> employees;
    private Log log = LogFactory.getLog(EmployeeServiceImpl.class.getName());

    @Override
    public ArrayList<Employee> getResultInfo() throws SQLException {
        // TODO Auto-generated method stub           
        PropertiesServiceImpl pro = new PropertiesServiceImpl();//配置文件信息读取类
        String conn =  pro.getConn();
        String user = pro.getUser();
        String password = pro.getPassword();
        String table = pro.getViewEmployee();
        JdbcUtils cs = new JdbcUtils(conn, user, password);//JDBC处理类
        String sqlString = "SELECT * FROM " + table;
        log.info("SQl:" + sqlString);
        employees = cs.queryEmployeeResult(sqlString);  
        return employees;
    }

    @Override
    public String getJsonResultInfo() {
        // TODO Auto-generated method stub
        String str = "";
        try {
            Json<Employee> deJson = new Json<Employee>();//JSON处理类
            str = deJson.JsonStr(this.getResultInfo());
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return str;
    }
}

 其中,JSON类的代码如下图所示,该类能将ArrayList转化为Json字符串,如下图所示,使用JSONArray需要引用几个jar包,点击这里下载 json-lib jar包

package Services;
import java.util.ArrayList;
import net.sf.json.JSONArray;

public class Json<E>{
    public String JsonStr(ArrayList<E> Lists){
        return JSONArray.fromObject(Lists).toString();
    }
}

编写ServicePublish类,里面有个main函数,用于本地发布测试。 

package Services;
import javax.xml.ws.Endpoint;

public class ServicePublish {   

    public static void main(String[] args){

        PropertiesServiceImpl pro = new PropertiesServiceImpl();
        String baseAddress = pro.getUrl();
        String employeeAddress = baseAddress + "Service/Employee";
        String companyAddress = baseAddress + "Service/Company";
        String deptAddress = baseAddress + "Service/Dept";
        String deptLeaderAddress = baseAddress + "Service/DeptLeader";

        Object implementorEmployee = new EmployeeServiceImpl();
        Object implementorCompany = new CompanyServiceImpl();
        Object implementorDept = new DeptServiceImpl();
        Object implementorDeptLeader = new DeptLeaderServiceImpl();
        Endpoint.publish(employeeAddress, implementorEmployee);     
        Endpoint.publish(companyAddress, implementorCompany);
        Endpoint.publish(deptAddress, implementorDept);
        Endpoint.publish(deptLeaderAddress, implementorDeptLeader);
    }
}

右键,选择"Run As" ——"Java Application",如下图所示,进行本地编译测试:

              

 

能出现XML说明成功了,到此为止本地运行成功:

 

下面将此WebService部署到TomCat上

首先需要添加一些jar包,总共25个,将这25个jar包添加到Tomcat路径的lib文件夹下。25个jar包的目录如下图所示:点击这里下载这25个jar包

                       

 

在web项目的WEB-INF目录下,创建sun-jaxws.xml配置文件,内容如下图所示:

然后还是在web项目的web-info目录下,创建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>BPMUserService</display-name>

<listener>
    <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>

<servlet>
    <servlet-name>EmployeeServiceImpl</servlet-name>
    <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>EmployeeServiceImpl</servlet-name>
    <url-pattern>/EmployeeServiceImpl</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>EmployeeServiceImpl</servlet-name>
    <url-pattern>/CompanyServiceImpl</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>EmployeeServiceImpl</servlet-name>
    <url-pattern>/DeptServiceImpl</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>EmployeeServiceImpl</servlet-name>
    <url-pattern>/DeptLeaderServiceImpl</url-pattern>
</servlet-mapping>

</web-app>

 

右键点击项目,将整个web项目导出为war包,如下图所示:

          

 

选择导出路径,还可以选择覆盖原文件:

                                         

将war包放入Tomcat的webapps文件夹下,如下图所示:

              

 

因为服务器中,默认的8080端口号已经被占用,所以我需要修改Tomcat默认的端口号, 在Tomcat的目录结构下找到conf文件夹,里面有个server.xml文件。

修改为8090端口,如下图所示:

                                          

启动Tomcat,并输入Url,Url是地址+web项目名称+服务名。即可访问服务:

posted @ 2020-12-28 09:06  不要摸我的腰  阅读(467)  评论(0编辑  收藏  举报