使用CXF和spring发布rest服务

来源:互联网 发布:淘宝上好评后怎么截图 编辑:程序博客网 时间:2024/05/29 10:54

创建web工程:

服务发布目录

web.xml:

<?xml version="1.0" encoding="UTF-8"?>  <web-app>      <!-- 配置Spring的配置文件 -->      <context-param>          <param-name>contextConfigLocation</param-name>          <param-value>WEB-INF/bean.xml</param-value>      </context-param>      <!-- 配置Spring的web Context监听器,将Spring与web工程集成在一起 -->      <listener>          <listener-class>              org.springframework.web.context.ContextLoaderListener          </listener-class>      </listener>      <!-- 配置CXF -->      <servlet>          <servlet-name>CXFServlet</servlet-name>          <display-name>CXF Servlet</display-name>          <servlet-class>              org.apache.cxf.transport.servlet.CXFServlet          </servlet-class>          <load-on-startup>1</load-on-startup>      </servlet>      <servlet-mapping>          <servlet-name>CXFServlet</servlet-name>          <url-pattern>/service/*</url-pattern>      </servlet-mapping>      <welcome-file-list>          <welcome-file>index.jsp</welcome-file>      </welcome-file-list>  </web-app>  

发布的服务在localhost:8080/test/service/ 可以看到
发布的rest服务

bean.xml:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:task="http://www.springframework.org/schema/task"     xmlns:util="http://www.springframework.org/schema/util"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:p="http://www.springframework.org/schema/p"    xmlns:jaxrs="http://cxf.apache.org/jaxrs"     xmlns:jaxws="http://cxf.apache.org/jaxws"    xsi:schemaLocation="    http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans.xsd    http://www.springframework.org/schema/aop    http://www.springframework.org/schema/aop/spring-aop.xsd    http://www.springframework.org/schema/task     http://www.springframework.org/schema/task/spring-task-3.0.xsd    http://www.springframework.org/schema/util    http://www.springframework.org/schema/util/spring-util-2.5.xsd    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.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/transports/http/configuration        http://cxf.apache.org/schemas/configuration/http-conf.xsd">    <!-- 将Bean托管给Spring -->        <bean id="hello" class="test.HelloImpl">    </bean>    <!-- 配置需要暴露的BeanService -->       <jaxrs:server id="restContainer2" address="/resthello">          <jaxrs:serviceBeans>              <ref bean="hello" />          </jaxrs:serviceBeans>       </jaxrs:server> </beans>

Hello.java:

package test;import javax.jws.WebMethod;import javax.jws.WebService;import javax.ws.rs.GET;import javax.ws.rs.Path;import javax.ws.rs.QueryParam;@Path("/hello")@WebServicepublic interface Hello {    @WebMethod(operationName = "output", action = "urn:Output")    @GET      @Path("/getName")    String output(); }

HelloImpl.java:

package test;import java.util.Date;import javax.jws.WebService;public class HelloImpl implements Hello{    @Override    public String output() {        // TODO Auto-generated method stub        System.out.println("output");        return "hello world";    }}

结合bean.xml中设置的address=”/resthello”,在Hello.java中设置的path可以通过路径:http://localhost:8080/test/service/resthello/hello/getName访问
rest服务调用

0 0
原创粉丝点击