使用CXF开发JAX-RS类型的WebService
1、JAXRSServerFactoryBean编程方式
访问方式:http://localhost:8080/cxf_spring_rest_server/ws/rest/student/querylist/001?_type=json
发布类:
public class StudentServer {
public static void main(String[] args) {
//使用jaxrsServerFactoryBean发布rest服务
JAXRSServerFactoryBean jaxrsServerFactoryBean = new JAXRSServerFactoryBean();
//设置rest的服务地址
jaxrsServerFactoryBean.setAddress("http://127.0.0.1:12345/rest");
//设置服务对象
jaxrsServerFactoryBean.setServiceBean(new StudentServiceImpl());
//设置资源 对象,如果有多个pojo资源 对象中间以半角逗号隔开
jaxrsServerFactoryBean.setResourceClasses(StudentServiceImpl.class);
//发布rest服务
jaxrsServerFactoryBean.create();
}
}
接口类:
@WebService
@Path("/student")
public interface StudentService { //查询学生信息
@GET //http的get方法
@Path("/query/{id}")//id参数通过url传递
@Produces(MediaType.APPLICATION_XML)//设置媒体类型xml格式
public Student queryStudent(@PathParam("id")long id); //查询学生列表
@GET //http的get方法
@Path("/querylist/{type}")
@Produces({"application/json;charset=utf-8",MediaType.APPLICATION_XML})//设置媒体类型xml格式和json格式
//如果想让rest返回xml需要在rest的url后边添加?_type=xml,默认为xml
//如果想让rest返回json需要在rest的url后边添加?_type=json
public List<Student> queryStudentList(@PathParam("type") String type); }
实现类:
public class StudentServiceImpl implements StudentService {
@Override
public Student queryStudent(long id) {
// 使用静态数据
Student student = new Student();
student.setId(id);
student.setName("张三");
student.setBirthday(new Date());
return student;
}
@Override
public List<Student> queryStudentList(String type) {
// 使用静态数据
List<Student> list = new ArrayList<Student>();
Student student1 = new Student();
student1.setId(1000l);
student1.setName("张三");
student1.setBirthday(new Date());
Student student2 = new Student();
student2.setId(1000l);
student2.setName("张三");
student2.setBirthday(new Date());
list.add(student1);
list.add(student2);
return list;
}
}
2、与spring整合
<!-- service -->
<bean id="studentService" class="cn.allan.ws.cxf.rest.service.StudentServiceImpl"/> <!-- 发布rest服务
使用jaxws:server和jaxws:endpoint可以发布服务
webservice地址=tomcat地址+cxf servlet的路径+/weather
-->
<jaxrs:server address="/rest">
<jaxrs:serviceBeans>
<ref bean="studentService"/>
</jaxrs:serviceBeans>
</jaxrs:server>
使用CXF开发JAX-RS类型的WebService的更多相关文章
- 用cxf开发restful风格的WebService
我们都知道cxf还可以开发restful风格的webService,下面是利用maven+spring4+cxf搭建webService服务端和客户端Demo 1.pom.xml <projec ...
- CXF 开发 WebService
什么是CXF: Apache CXF = Celtix + Xfire 支持多种协议: SOAP1.1,1.2 XML/HTTP CORBA(Common Object Request Broker ...
- 3.使用CXF开发webService
CXF 简介 关于 Apache CXF Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache ...
- webservice原理及基于cxf开发的基本流程
一.SOA和webservice SOA(service-Oriented Architecture)是面向服务的架构,是一个组件模型,它将应用程序的不同功能单元(称为服务)通过这些服务之间定义良好的 ...
- struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例
Cxf + Spring+ myeclipse+ cxf 进行 Webservice服务端开发 使用Cxf开发webservice的服务端项目结构 Spring配置文件applicationCont ...
- 使用cxf开发webservice应用时抛出异常
在使用cxf开发webservice应用时,报出了类似下面的错误 JAXB: [javax.xml.bind.UnmarshalException: unexpected element (uri:& ...
- Spring boot+CXF开发WebService
最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...
- Spring boot+CXF开发WebService Demo
最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...
- (二)使用CXF开发WebService服务器端接口
CXF作为java领域主流的WebService实现框架,Java程序员有必要掌握它. CXF主页:http://cxf.apache.org/ 简介:百度百科 今天的话,主要是用CXF来开发下Web ...
- 【WebService】使用CXF开发WebService(四)
CXF简介 Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.CXF 继承了 Celtix ...
随机推荐
- (MVC)javaBaen+jsp+servlet对数据的操作
运用MVC对数据进行一些简单的处理,基本实现数据的增删改查,达到前端和后台的数据之间的交互. 1.开始界面 <%@page import="com.zdsofe.work.Studen ...
- webpack2 前篇
webpack2 前篇 #webpack 前两天用了一天半时间琢磨了下webpack2,想起去年这时候,面对webpack1那样恶心的文档,前前后后搞了好几次才摸索清楚,那真是吐了. 划重点 其实we ...
- java考试易错题大全
常见的Java问题 1.什么是Java虚拟机?为什么Java被称作是"平台无关的编程语言"? Java虚拟机是一个可以执行Java字节码的虚拟机进程.Java源文件被编译成能被Ja ...
- hdu--1421--dp--搬寝室
注意:dp[i][j]初始化的时候不能小于0xffffff明显大于2^15但是吧再小就WA了 #include<iostream> #include<algorithm> #i ...
- Mybatis源码分析-StatementHandler
承接前文Mybatis源码分析-BaseExecutor,本文则对通过StatementHandler接口完成数据库的CRUD操作作简单的分析 StatementHandler#接口列表 //获取St ...
- SVN:Cleanup failed to process the following paths
频繁使用SVN,于是乎玩坏了.用了一下clearup,结果爆了如题错误.查了一下,是有文件被加锁了,位置在项目根目录 .svn下的wc.db 里,需用专门工具才能看到里面.就是个数据库,里面有很多表. ...
- Linux操作系统-命令-netstat
# 之前已经写过了3篇与"性能测试"有关系的Linux命令,它们分别是free.top.vmstat # 接下来还需要把另外2个命令也写下来:netstat和iostat 最近认真地读了1篇关于"定位 ...
- oracle-使用数据泵对不同用户和不同表空间的数据迁移
oracle-使用数据泵对不同用户和不同表空间的数据迁移 ---------------------------------------------------2013/11/13 expdp和imp ...
- Linux/Unix监控其他用户和信号
--Linux/Unix监控其他用户和信号 ------------------------------------------------------2013/10/27 查看有哪些用户登录 w ...
- 关于mysql增删改查的基本操作
1.注释语法:--,#2.后缀是.sql的文件是数据库查询文件3.保存查询4.在数据库里面 列有个名字叫字段 行有个名字叫记录CRUD操作:create 创建(添加)read 读取update 修 ...