運用websrevice的CXF框架下的restful開發簡單實例

一:基本jar包及cxf相關jar包座標

    

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>
<!--web-->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--測試-->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <!--只在test測試裏面運行-->
  <scope>test</scope>
</dependency>

<!--加入jsp集成-->
<dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
<dependency>
  <groupId>javax.servlet.jsp.jstl</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
</dependency>

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <version>2.5</version>
</dependency>

<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
  <groupId>taglibs</groupId>
  <artifactId>standard</artifactId>
  <version>1.1.2</version>
</dependency>

<!--自動將對象返回成json格式-->
<dependency>
  <groupId>com.fasterxml.jackson.jaxrs</groupId>
  <artifactId>jackson-jaxrs-json-provider</artifactId>
  <version>2.5.4</version>
</dependency>

  <!--導入httpClient用來調用restfulWebservice服務-->
  <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>fluent-hc</artifactId>
      <version>4.5.2</version>
  </dependency>
<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.5.2</version>
</dependency>

<!--restful開發相關jar包-->
<dependency>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-rt-frontend-jaxrs</artifactId>
  <version>3.1.6</version>
</dependency>

<dependency>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-rt-transports-http</artifactId>
  <version>3.1.6</version>
</dependency>

<dependency>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-core</artifactId>
  <version>3.1.6</version>
</dependency>
<dependency>
  <groupId>com.colobu</groupId>
  <artifactId>fastjson-jaxrs-json-provider</artifactId>
  <version>0.1.0</version>
</dependency>

<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.9</version>
</dependency>

<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
<dependency>
  <groupId>net.sf.json-lib</groupId>
  <artifactId>json-lib</artifactId>
  <version>2.4</version>
  <classifier>jdk15</classifier>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
<dependency>
  <groupId>commons-beanutils</groupId>
  <artifactId>commons-beanutils</artifactId>
  <version>1.9.3</version>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
<dependency>
  <groupId>commons-collections</groupId>
  <artifactId>commons-collections</artifactId>
  <version>3.2.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
<dependency>
  <groupId>commons-lang</groupId>
  <artifactId>commons-lang</artifactId>
  <version>2.6</version>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
<dependency>
  <groupId>commons-httpclient</groupId>
  <artifactId>commons-httpclient</artifactId>
  <version>3.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
  <groupId>commons-logging</groupId>
  <artifactId>commons-logging</artifactId>
  <version>1.1.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/net.sf.ezmorph/ezmorph -->
<dependency>
  <groupId>net.sf.ezmorph</groupId>
  <artifactId>ezmorph</artifactId>
  <version>1.0.6</version>
</dependency>

二:開發webService相關業務邏輯層

    1:service接口

    
package com.csc.webservice;


import javax.jws.WebService;

@WebService
public interface UserwsService {
    public String  queryMSG(String host);

}

    2:service實現類

        

package com.csc.webservice;

import net.sf.json.JSONObject;
import com.csc.entity.User;

import javax.jws.WebService;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;


@WebService

public class UserwsServiceImpl implements UserwsService {
    @GET
    @Path("/bothway/{host}/")
    @Produces(value={"application/json;charset=UTF-8"})
    public String queryMSG(@PathParam("host") String host){
        System.out.println(host);
        User user = new User("tom","tom123","88888888","18888888888","18888888886");
        JSONObject json = JSONObject.fromObject(user);
        String  str = json.toString();
        return str;
    }
}
三:新建cxf相關xml引用jarxs.xsd並配置

        

<?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:jaxrs="http://cxf.apache.org/jaxrs"
       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">
    <!--管理服務實現類-->
    <bean id="userwsService" class="com.csc.webservice.UserwsServiceImpl"/>
    <!--發佈 rest webservice-->
    <jaxrs:server address="/msgapi" serviceClass="com.csc.webservice.UserwsService">
        <jaxrs:serviceBeans>
            <ref bean="userwsService"/>
        </jaxrs:serviceBeans>
    </jaxrs:server>
</beans>

四:配置web.xml

    

 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:spring-cxf.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

<!--配置CXFservlet-->
<servlet>
  <servlet-name>cxf</servlet-name>
  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>cxf</servlet-name>
  <url-pattern>/cxf/*</url-pattern>
</servlet-mapping>
好了接下倆只需要把項目打包部署到tomcat上啓動服器就可以發佈了,然後根據上面這種底色的路徑就可以訪問了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章