最简单的cxf restful webservice Demo(包括与spring集成)

package 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;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

}


package service;

import javax.jws.WebService;
import javax.ws.rs.Consumes;
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 pojo.Student;

@WebService
@Path("/student")
public interface StudentService {

    @GET
    @Path("query/{id}")
    @Produces(MediaType.APPLICATION_JSON)//这里可以自行定义返回的数据格式json xml等
    public Student queryStudent(@PathParam("id")long id);
}


package service;

import java.util.Date;




import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;

import pojo.Student;

public class StudentServiceImpl implements StudentService {
    @Override
    public Student queryStudent(long id) {
        Student student=new Student();
        student.setId(1);
        student.setName("yang");
        student.setBirthday(new Date());
        return student;
    }
    //为了省事直接在此类运行一个main方法发布服务
    public static void main(String[] args) {
        JAXRSServerFactoryBean JaxRSServerFactoryBean=new JAXRSServerFactoryBean();
        JaxRSServerFactoryBean.setAddress("http://127.0.0.1:12347/rest");
        JaxRSServerFactoryBean.setServiceBean(new StudentServiceImpl());
        JaxRSServerFactoryBean.setResourceClasses(StudentServiceImpl.class);
        JaxRSServerFactoryBean.create();

    }
}



浏览器访问http://127.0.0.1:12347/rest/student/query/001
返回json格式数据
{"student":{"birthday":"2015-12-15T10:29:06.381+08:00","id":1,"name":"yang"}}


与spring集成
application.xml
version="1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                            http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
                            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
                            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">


"studentService" class="service.StudentServiceImpl">
"/restful">

<ref bean="studentService"/>







web.xml

version="1.0" encoding="UTF-8"?>
"http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  cxf_restful



  
  
  contextConfigLocation
  /WEB-INF/classes/applicationContext.xml
  

  
  class>
  org.springframework.web.context.ContextLoaderListener
  class>
  

  
  cxf
  class>org.apache.cxf.transport.servlet.CXFServletclass>
  1
  
 
  
  cxf
  /ws/*
  


将该web项目发布到tomcat下 访问该URL   http://localhost:8080/cxf_restful/ws/restful/student/query/001

返回结果
{"student":{"birthday":"2015-12-15T10:42:56.044+08:00","id":1,"name":"yang"}}

所需jar包不方便提供大家自行下载







你可能感兴趣的:(webservice)