CXF 发布rest服务
1.1 什么是rest服务
REST 是一种软件架构模式,只是一种风格,rest服务采用HTTP 做传输协议,REST 对于HTTP 的利用实现精确的资源定位。
Rest要求对资源定位更加准确,如下:
非rest方式:http://ip:port/queryUser.action?userType=student&id=001
Rest方式:http://ip:port/user/student/query/001
Rest方式表示互联网上的资源更加准确,但是也有缺点,可能目录的层级较多不容易理解。
REST 是一种软件架构理念,现在被移植到Web 服务上,那么在开发Web 服务上,偏于面向资源的服务适用于REST,REST 简单易用,效率高,SOAP 成熟度较高,安全性较好。
注意:REST 不等于WebService,JAX-RS 只是将REST 设计风格应用到Web 服务开发上。
1.2 发布rest服务
1.2.1 需求:
发布查询学生信息的服务,以json和xml数据格式返回。
1.2.2 pojo
@XmlRootElement(name="student")
public class Student {
private long id;
private String name;
private Date birthday;
1.2.3 SEI
@WebService
@Path("/student")
public interface StudentService {
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("/query/{id}")
public Student queryStudent(@PathParam("id")long id)throws Exception;
@GET
@Produces({"application/json;charset=utf-8",MediaType.APPLICATION_XML})
@Path("/querylist/{id}")
public List<Student> queryStudentList(@PathParam("id")long id)throws Exception;
}
上边代码中:
queryStudent方法以xml格式发布
queryStudentList方法以json和xml两种格式发布。
1.2.4 SEI实现类
public class StudentServiceImpl implements StudentService {
@Override
public Student queryStudent(long id) throws Exception {
Student student = new Student();
student.setId(100000l);
student.setName("张三");
student.setBirthday(new Date());
return student;
}
@Override
public List<Student> queryStudentList(long id) throws Exception {
Student student1 = new Student();
student1.setId(100000l);
student1.setName("李四");
student1.setBirthday(new Date());
Student student2 = new Student();
student2.setId(100000l);
student2.setName("张三");
student2.setBirthday(new Date());
List<Student> list = new ArrayList<Student>();
list.add(student1);
list.add(student2);
return list;
}
}
1.2.5 程序代码发布
public class RestServer {
public static void main(String[] args) {
JAXRSServerFactoryBean jaxrsServerFactoryBean = new JAXRSServerFactoryBean();
//rest地址
jaxrsServerFactoryBean.setAddress("http://127.0.0.1:12345/rest");
//设置SEI实现类
jaxrsServerFactoryBean.setResourceClasses(StudentServiceImpl.class);
jaxrsServerFactoryBean.create();
}
}
1.2.6 Spring配置文件发布
<!-- rest服务发布 -->
<jaxrs:server address="/rest">
<jaxrs:serviceBeans>
<bean class="cn.itcast.ws.cxf.rest.server.StudentServiceImpl"/>
</jaxrs:serviceBeans>
1.2.7 测试
queryStudent方法测试:
http://127.0.0.1:8080/工程名/ws/rest/student/query/1
queryStudentList方法测试:
返回json
http://127.0.0.1:8080/工程名/ws/rest/student/querylist/1?_type=json
返回xml
http://127.0.0.1:8080/工程名/ws/rest/student/querylist/1?_type=xml
CXF 发布rest服务的更多相关文章
- CXF发布webService服务以及客户端调用
这篇随笔内容是CXF发布webService服务以及客户端调用的方法 CXF是什么? 开发工作之前需要下载CXF和安装 下载地址:http://cxf.apache.org 安装过程: <1&g ...
- JAVAEE——BOS物流项目07:WebService入门、apache CXF入门、基于CXF发布CRM服务
1 学习计划 1.WebService入门 n 什么是WebService n 调用网络上的WebService服务 n SOAP和WSDL概念 n 基于JDK1.7发布一个简单的WebService ...
- 使用CXF发布WebService服务简单实例
一.说明: 前面介绍了使用axis2来发布Webservice服务,现在介绍一种更popular,更高效的Webservice服务发布技术:CXF Apache CXF = Celtix + XFir ...
- Web Service——CXF发布REST服务
1. 什么是REST REST,英文representational state transfer(表象性状态转变)或者表述性状态转移,REST是web服务的一种架构风格,使用HTTP.URI.XML ...
- cxf发布 webservice服务
导包 antlr-2.7.7.jar aopalliance-1.0.jar asm-3.3.jar commons-collections-3.2.1.jar commons-lang-2.6.ja ...
- 使用CXF发布webservice服务及注意要点
一.概念 1.什么是webservice Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML标准来描述.发布.发现.协调和配置这些应用程序,用 ...
- 客户端调用cxf发布的服务
import java.util.ArrayList; import java.util.List; import javax.xml.namespace.QName; import org.apac ...
- 使用Cxf发布Webservice服务,如果待发布的接口中有重载方法,怎么处理??
使用 @WebMethod(operationName="multiParamByName") 重新指定名字. http://bbs.csdn.net/topics/270059 ...
- WebService--使用 CXF 开发 REST 服务
现在您已经学会了如何使用 CXF 开发基于 SOAP 的 Web 服务,也领略了 Spring + CXF 这个强大的组合,如果您错过了这精彩的一幕,请回头看看这篇吧: Web Service 那点事 ...
随机推荐
- varnish安装和配置
实验环境:CentOS7 Varnish是高性能开源的反向代理服务器和HTTP缓存服务器. #varnish服务器:172.16.252.142 [root@varnish localhost]#yu ...
- 《Spring实战》系列之Bean的装配-Days02
2.1 回顾 对于我第一天在bean的装配中写的,是一些基本的语法或者是Spring本身的一些规定,但是我没有对此进行深究.接下来就让我们仔细的讨论一下细节问题.和传统的类的定义和方法的调用做一些比较 ...
- spring 4.0 注解数据验证1
通常情况下,数据验证都分为前台验证,后台验证.并且前台JS验证是肯定有的,那么其实验证的错误信息根本不必通过后台传过去,哪怕就是想国际化,前台JS也能够胜任. 如果前台验证足够了,那么如果还有不正确的 ...
- 转:JMeter整合InfluxDB,Grafana让测试结果实时显示
软件版本: apache-jmeter-2.13.tgz grafana-2.1.1-1.x86_64.rpm influxdb-0.8.8-1.x86_64.rpm 虽然官方不在支持influxdb ...
- 使用python ftplib包递归下载文件夹及文件
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2018-06-11 09:35:49 # @Author : Yaheng Wang ...
- 阶段3-团队合作\项目-网络安全传输系统\sprint2-线程池技术优化
之前问题的存在,之前只是用一个客户端在与服务器进行连接,当多个客户端进行连接的时候会连接不上处于等待状态,说明以前我们的服务器只能同时处理一个请求,故需要修改 服务器: 单发:初始化--等待客户端连接 ...
- cc和gcc
cc就是一个链接文件连接到gcc中.只不过cc是unix中常用的编辑工具,而在linux中用的gcc.有一些在unix中写好的程序要放在linux中,所以要指定命令cc为gcc,其实一样.用where ...
- 理解linux服务器mcelog如何工作
What are Machine Check Exceptions (or MCE)? A machine check exception is an error dedected by your s ...
- HTML5学习笔记(七)WebSocket
WebSocket是HTML5开始提供的一种在单个 TCP 连接上进行全双工通讯的协议.在WebSocket API中,浏览器和服务器只需要做一个握手的动作 浏览器通过 JavaScript 向服务器 ...
- 【Java】NIO中Selector的创建源码分析
在使用Selector时首先需要通过静态方法open创建Selector对象 public static Selector open() throws IOException { return Sel ...