14_CXF发布REST服务
来源:互联网 发布:苹果平板淘宝开店在哪 编辑:程序博客网 时间:2024/06/05 10:58
http://www.cnblogs.com/HigginCui/p/5845199.html
【rest服务】
REST服务是一种软件架构模式,只是一种风格。REST服务采用HTTP做传输协议。
REST对于HTTP的利用分为以下两种:
一.资源定位
REST要求对方资源定位更加准确,如下:
非REST方式:http://127.0.0.1:12345/queryUser.action?userType=student&id=001
REST 方式:htpp://127.0.0.1:12345/student/001
REST方式表示互联网上的资源更加准确,但也有缺点,当目录的层级较多时会导致较难理解。
二.资源操作
利用HTTP的GET、POST、PUT、DELETE四种操作来表示数据库操作的SELECT、UPDATE、INSERT、DELETE操作。
比如:
1.查询学生的方法,设置HTTP的请求方法为GET,
url如下:http://127.0.0.1:12345/user/student/001
2.添加学生的方法,设置HTTP的请求方法为PUT,
url如下:http://127.0.0.1:12345/user/student/001/张三/...
REST常用于资源定位,资源的操作方式较少使用。
【使用场景】
REST是一种软件架构理念,现在被移植到web服务上,那么在开发Web服务上,偏于面向资源的服务的适用于REST,REST简单易用,效率高,SOAP成熟度较高,安全性好。
注意:REST不等于WebService,JAX-RS只是将REST设计风格应用到Web服务上。
【代码需求】
查询学生信息,传输参数为学生信息id,在url中定义,如http://127.0.0.1:12345/user/student/001,
其中001就是参数。
注意:REST一般返回结果格式:XML 或 JSON
【工程截图】
【Student.java】
package com.Higgin.ws.cxf.rest.pojo;import java.util.Date;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement(name="student")public class Student { private long id; private String name; private Date birthday; //忽略 get/set 方法.....}
【StudentService.java】
package com.Higgin.ws.cxf.rest.service;import java.util.List;import javax.jws.WebService;import javax.ws.rs.GET;import javax.ws.rs.Path;import javax.ws.rs.PathParam;import javax.ws.rs.Produces;import javax.ws.rs.core.MediaType;import com.Higgin.ws.cxf.rest.pojo.Student;@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 @Path("/queryList/{type}") //设置媒体类型为XML格式和JSON格式 @Produces({"application/json;charset=utf-8",MediaType.APPLICATION_XML}) /* 如果想让rest返回xml,需要在rest的url后边添加 ?_type=xml,默认为xml 如果想让rest返回json.需要在rest的url后边添加?_type=json*/ public List<Student> queryStudentList(@PathParam("type") String type);}
【StudentServiceImpl.java】
package com.Higgin.ws.cxf.rest.service;import java.util.ArrayList;import java.util.Date;import java.util.List;import com.Higgin.ws.cxf.rest.pojo.Student;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 student1=new Student(); student1.setId(1001); student1.setName("张三"); student1.setBirthday(new Date()); Student student2=new Student(); student2.setId(1002); student2.setName("张三"); student2.setBirthday(new Date()); list.add(student1); list.add(student2); return list; }}
【StudentServer.java】
package com.Higgin.ws.cxf.rest.server;import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;import com.Higgin.ws.cxf.rest.service.StudentServiceImpl;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(); }}
【启动服务】使用Java Application启动main方法
1.在浏览器中输入 http://127.0.0.1:12345/rest/student/query/666
2.在浏览器中输入 http://127.0.0.1:12345/rest/student/queryList/123
3.在浏览器中输入 http://127.0.0.1:12345/rest/student/queryList/001?_type=xml
4.在浏览器中输入 http://127.0.0.1:12345/rest/student/queryList/001?_type=json
- 14_CXF发布REST服务
- Tuscany发布REST服务
- springmvc发布rest服务
- 909422229_CXF+myeclipse进行发布接口
- ArcGIS Server 发布 REST 服务
- ArcGIS Server 发布 REST 服务
- Jersey 发布rest服务接口
- koala4以rest方式发布webservice服务
- 使用CXF和spring发布rest服务
- OSGI系列 Servlet-Rest发布Http服务
- springmvc发布rest服务(springmvc配置补充)
- Jetty集成Resteasy发布rest服务Demo
- webservice--CXF发布REST的服务
- Java Rest WebService 三步服务发布
- 使用jersey2.X发布rest服务
- dubbox 发布rest服务调用过程记录
- ArcGIS server 9.3 无法发布rest网络路径分析服务
- Cxf发布Rest服务时,利用WebClient传递QueryParam参数
- Linux下静态库_库的基本概念;如何生成静态库动态库;nm查看库中包含那些函数、ar生成静态库,查看库中包含那些.o文件、ldd查看程序依赖的.so文件;gcc/g++与库相关的参数-L,-l,-f
- 编译期成员函数检查
- zzuli2159 布线 贪心
- 从R-CNN到Faster-RCNN(上)
- 数据库基础
- 14_CXF发布REST服务
- 如何配置python的运行环境?
- Ubuntu14.04上配置caffe
- nodejs浏览器跨域问题解决
- 学习SpringMVC——配置文件和第一个SpringMVC应用
- 异常
- CXF发布restful WebService的入门例子(客户端)
- shell常用代码块
- BZOJ 3036 绿豆蛙的归宿