14_CXF发布REST服务

【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

原文地址:https://www.cnblogs.com/HigginCui/p/5845199.html