当前位置: 首页 > news >正文

初入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  

转载于:https://www.cnblogs.com/fliay/p/7399236.html

相关文章:

  • 如何使用 URLOpenStream 函数 - 回复 天泽 的问题
  • 超强IIS站点工具一键设置PHP,支持多个PHP同时运行
  • 0101 项目经理的精神病(一)孤独
  • 锋利的jQuery-----读书笔记
  • Windows Mobile 6.5 配置环境,数据库访问,部署简单实例
  • KMP 算法(1):如何理解 KMP
  • 自己编写站内搜索
  • E. XOR and Favorite Number
  • VC精确计算代码执行时间
  • JS 时间戳转星期几 AND js时间戳判断时间几天前
  • 《软件设计精要与模式》第二版出版
  • ES2017异步函数现已正式可用
  • Windows2008server R2 组策略批量更改本地管理员密码
  • 常用国名与首都(英文)
  • autodesk fbx sdk sample里面的工程无法调试解决方法
  • 【跃迁之路】【585天】程序员高效学习方法论探索系列(实验阶段342-2018.09.13)...
  • android百种动画侧滑库、步骤视图、TextView效果、社交、搜房、K线图等源码
  • Android路由框架AnnoRouter:使用Java接口来定义路由跳转
  • Centos6.8 使用rpm安装mysql5.7
  • iOS | NSProxy
  • JavaScript学习总结——原型
  • js ES6 求数组的交集,并集,还有差集
  • JS专题之继承
  • JS字符串转数字方法总结
  • Laravel5.4 Queues队列学习
  • Mybatis初体验
  • Netty 框架总结「ChannelHandler 及 EventLoop」
  • node和express搭建代理服务器(源码)
  • Quartz实现数据同步 | 从0开始构建SpringCloud微服务(3)
  • ReactNativeweexDeviceOne对比
  • 分享一个自己写的基于canvas的原生js图片爆炸插件
  • 数据库写操作弃用“SELECT ... FOR UPDATE”解决方案
  • 3月7日云栖精选夜读 | RSA 2019安全大会:企业资产管理成行业新风向标,云上安全占绝对优势 ...
  • C# - 为值类型重定义相等性
  • ionic异常记录
  • Nginx惊现漏洞 百万网站面临“拖库”风险
  • ​DB-Engines 11月数据库排名:PostgreSQL坐稳同期涨幅榜冠军宝座
  • #{} 和 ${}区别
  • #HarmonyOS:基础语法
  • (笔试题)合法字符串
  • (转)iOS字体
  • (转)setTimeout 和 setInterval 的区别
  • (转)大型网站架构演变和知识体系
  • (转)自己动手搭建Nginx+memcache+xdebug+php运行环境绿色版 For windows版
  • **CI中自动类加载的用法总结
  • .bat批处理出现中文乱码的情况
  • .Net Remoting(分离服务程序实现) - Part.3
  • .NET框架设计—常被忽视的C#设计技巧
  • /bin/rm: 参数列表过长"的解决办法
  • @configuration注解_2w字长文给你讲透了配置类为什么要添加 @Configuration注解
  • @NestedConfigurationProperty 注解用法
  • [1181]linux两台服务器之间传输文件和文件夹
  • [2016.7 Day.4] T1 游戏 [正解:二分图 偏解:奇葩贪心+模拟?(不知如何称呼不过居然比std还快)]
  • [Angularjs]asp.net mvc+angularjs+web api单页应用之CRUD操作
  • [Apio2012]dispatching 左偏树