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

本文介绍了一个简单的RESTful服务实现方式,并演示了如何使用CXF框架与Spring集成来快速部署RESTful服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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">


<bean id="studentService" class="service.StudentServiceImpl"></bean>
<jaxrs:server address="/restful">
<jaxrs:serviceBeans>
<ref bean="studentService"/>
</jaxrs:serviceBeans>
</jaxrs:server>
</beans>




web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="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">
  <display-name>cxf_restful</display-name>



  <!--加载spring容器 -->
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
  </context-param>

  <listener>
  <listener-class>
  org.springframework.web.context.ContextLoaderListener
  </listener-class>
  </listener>

  <servlet>
  <servlet-name>cxf</servlet-name>
  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
  </servlet>
 <!-- 本系统webservice的路径必须以/ws/开头 -->
  <servlet-mapping>
  <servlet-name>cxf</servlet-name>
  <url-pattern>/ws/*</url-pattern>
  </servlet-mapping>
</web-app>

将该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包不方便提供大家自行下载







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值