zoukankan      html  css  js  c++  java
  • 初入WebService

    1. 搭建webservice需要用到的jar
    2. applicationContext.xml配置文件

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
      	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
      	xmlns:cxf="http://cxf.apache.org/core" xmlns:jaxws="http://cxf.apache.org/jaxws"
      	xmlns:jaxrs="http://cxf.apache.org/jaxrs"
      
      	xsi:schemaLocation="
      	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
      	http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd 
      	http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
      	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
      
      	<jaxrs:server id="employeeServiceServer" address="/employeeservice">
      		<jaxrs:serviceBeans>
      			<ref bean="employeeService" />
      		</jaxrs:serviceBeans>
      		<jaxrs:providers>
      			<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
      		</jaxrs:providers>
      	</jaxrs:server>
      
      	<bean id="employeeService" class="service.EmployeeService" />
      </beans>
      

        

    3. 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>employee-service</display-name>
      
      <context-param>
      	<param-name>contextConfigLocation</param-name>
      	<param-value>/WEB-INF/applicationContext.xml</param-value>
      </context-param>
      <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      
          <servlet>
           	<servlet-name>CXFServlet</servlet-name>
              <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
          </servlet>
      	<servlet-mapping>
              <servlet-name>CXFServlet</servlet-name>
              <url-pattern>/services/*</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>
      

        

    4. model/employee.java
      package model;
      
      import java.io.Serializable;
      
      public class Employee implements Serializable
      {
          /**
      	 * 
      	 */
      	private static final long serialVersionUID = 5954986571237608774L;
      	private String id;
          private String code;
          private String name;
      
          public String getId()
          {
              return id;
          }
      
          public void setId(String id)
          {
              this.id = id;
          }
      
          public String getCode()
          {
              return code;
          }
      
          public void setCode(String code)
          {
              this.code = code;
          }
      
          public String getName()
          {
              return name;
          }
      
          public void setName(String name)
          {
              this.name = name;
          }
      
      	@Override
      	public String toString() {
      		return "Employee [code=" + code + ", id=" + id + ", name=" + name + "]";
      	}
      
      }
      
    5. service/employeeservice.java

      package service;
      
      import java.util.ArrayList;
      import java.util.List;
      
      import javax.ws.rs.Consumes;
      import javax.ws.rs.GET;
      import javax.ws.rs.Path;
      import javax.ws.rs.Produces;
      
      import model.Employee;
      
      public class  EmployeeService {
      	
      	@GET
          @Path("/employees")
          @Consumes("application/json")
          @Produces("application/json")
      	public List<Employee> findallEmployee(){
      		return initData();
      	}
      	 
      	public List<Employee> initData(){
      		List<Employee> list = new ArrayList<Employee>();
      		for(int i=0;i<10;i++){
      			Employee emp = new Employee();
      			emp.setId("id"+i);
      			emp.setName("员工"+i);
      			emp.setCode("code"+i);
      			list.add(emp);
      		}
      		return list;
      	}
      	
      	public static void main(String[] args) {
      		EmployeeService a = new EmployeeService();
      		for(Employee e:a.findallEmployee()){
      			System.out.println(e);
      		}
      	}
      	
      }
      

        

        部署项目,打开浏览器输入地址http://ip:port/项目名称/services/employeeservice/employees  

  • 相关阅读:
    简单的ajax的结构
    Sencha touch 中的一段源码匿名中定义Function并调用
    js使用闭包时,内部函数是直接访问外部函数的实际变量而非复制一份新变量
    rd /q /s 删除文件
    两种定义Function的方式 JavaScript
    DOC @echo off call
    Function 对象,javascript中双括号的运行机制
    抽象的力量
    Hash算法,及HashMap使用
    如何使用EnumSet实现基于bit field的enum set?
  • 原文地址:https://www.cnblogs.com/fliay/p/7399236.html
Copyright © 2011-2022 走看看