java webservice 开发方法(REST方式)

来源:互联网 发布:gis空间数据分析 编辑:程序博客网 时间:2024/06/14 07:37

0.webservice的特性

0.1.一般都支持SOAP1.1和SOAP1.2,目前流行REST style的webservice,也即不但支持“”WS-*" style接口,也支持REST/POX style的接口。

0.2.速度快、轻量级、热部署、稳定、适应性强、异步调用、组件式部署,支持WSDL等。

1.开发环境准备

1.1.eclipse

1.2.jersey

1.3.tomcat

1.4.mybatis

1.5.mysql

2.开发

2.1.在eclipse中建立Web project,名为ZKAppService

2.2.将 jersey-bundle-1.19.4.jar 放入/ZJAppService/WebContent/WEB-INF/lib/

2.3.创建类CheckLoginNamePassWord

package com.fivesuo.zkapp.webservice;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

/**
 * WebService implementation class CheckLoginNamePassWord
 */
@Path("/checkloginnamepassword")
public class CheckLoginNamePassWord {
@Context
    HttpServletRequest request;
@Context
    HttpServletResponse response;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getResult(@QueryParam("name") String name){
    return "This is Get style:"+request.getParameter("name")
;
    }

    @POST
    @Produces(MediaType.TEXT_PLAIN)
    public String postResult(@QueryParam("name") String name){
    return "This is Post style";
    }
}

3.打包:在eclipse中的ZKAppService项目上点击鼠标右键,export WAR file,并发布到tomcat的ROOT下

4.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>ZKAppService</display-name>
  <servlet>
    <servlet-name>ZKAppService</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>com.fivesuo.zkapp.webservice</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>ZKAppService</servlet-name>
    <url-pattern>/ZKAppService.svc/*</url-pattern>
  </servlet-mapping>
  <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>
</web-app>

5.确认结果:http://localhost:8080/ZKAppService/checkloginnamepassword?name=gxzk

结果为:This is Get style:gxzk


原创粉丝点击