java webservice 開發方法(REST方式)

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章