`
haytalent
  • 浏览: 17209 次
  • 性别: Icon_minigender_1
  • 来自: 未知国度
社区版块
存档分类
最新评论

cxf、spring集成webservice 配置restful方式

阅读更多

刚刚配置完spring集成cxf使用restful方式部署webservice,整个过程感觉就是爽和简单,欢迎抛砖引玉

第一步:当然是下载jar包了

            使用到的jar有以下:

            1、spring jar包我就不说了,地球人都知道

            2、cxf-2.6.9.jar 最新版是2.7X版本,我开始下的2.7版本,发现支持的JSR是2.0,起码需要JSR-339.api.jar,但是我下的是jsr311-api-1.1.1.jar,只能支持2.7以下版本,后来又重新下了cxf2.6.9在这里记录下避免忘记了。

            3、jsr311-api-1.1.1.jar

            4、geronimo-servlet_3.0_spec-1.0.jar

            5、neethi-3.0.2.jar

第二步: 配置项目中web.xml

               <servlet>

    <servlet-name>CXFService</servlet-name>

    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>

</servlet>

 

<servlet-mapping>

    <servlet-name>CXFService</servlet-name>

    <url-pattern>/*</url-pattern>

</servlet-mapping>

 

 

第三步:

         新建appalicationcontext_cxf.xml

         内容如下

         <?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:jaxws="http://cxf.apache.org/jaxws"

    xmlns:jaxrs="http://cxf.apache.org/jaxrs"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.springframework.org/schema/beans 

    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context-3.0.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">

<import resource="classpath:META-INF/cxf/cxf.xml"/>

<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>

<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

 

 

<bean id="restSample" class="com.community.webservice.RESTSampleSource"/>

 

 

<!-- 这里的地址很重要,客户端需要通过这个地址来访问WebService -->

<jaxrs:server id="restServiceContainer" address="/rest">

    <jaxrs:serviceBeans>

        <ref bean="restSample" />

    </jaxrs:serviceBeans>

    <jaxrs:extensionMappings>

        <entry key="json" value="application/json" />

        <entry key="xml" value="application/xml" />

    </jaxrs:extensionMappings>

    <jaxrs:languageMappings>

           <entry key="en" value="en-gb"/>  

    </jaxrs:languageMappings>

</jaxrs:server>

 

</beans>

 

 

 

下面就是例子

     新建Sample接口类

    @Path(value = "/sample")

public interface Sample {

 

@GET

@Path("/bean/{id}")

@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })

public String getBean(@PathParam("id") int id);

 

@GET

@Path("/list/{id}")

@Produces({ "application/xml","application/json" })

public String getBean2(@PathParam("id") int id);

}

 

新建Sample接口的实现类

@Path(value = "/sample")

public class RESTSampleSource implements Sample {

 

@Override

@GET

@Path("/bean/{id}")

@Produces({ "application/xml", "application/json" })

public String getBean(@PathParam("id") int id) {

return "<xml>1</xml>";

}

 

 

@Override

@GET

@Path("/list")

@Produces({ "application/xml","application/json" })

public String getBean2(@PathParam("id") int id) {

return "{id:'123'}";

}

 

}

 

好了,启动发布

使用

http://localhost:8080/cxfWebservice/rest/sample/bean/123    默认返回xml

http://localhost:8080/cxfWebservice/rest/sample/list?id=1&_type=json   加入_type=json则可以返回json数据

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics