加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 22:27:27 所属栏目:安全 来源:网络整理
导读: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;
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包不方便提供大家自行下载 

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读