天天看點

CXF架構的restful webservice 環境配置(一) 服務端

1.導入CXF的jar包

2.寫對外釋出的接口

package com.service;

public interface Service {

public String SendMail(String xml);//POST方法參數正常寫法

public String getMail(@PathParam("appID")String appID){};//GET方法的路徑參數要這樣寫

}

3.寫實作類

POST方法

package com.service.impl;

import javax.ws.rs.Consumes;

import javax.ws.rs.POST;

import javax.ws.rs.Path;

import javax.ws.rs.Produces;

import com.service.Service;

@Path("/xml")   //釋出路徑參數,在配置檔案中配置的

@Produces("**")

public String SendMail(String xml) {

System.out.println("收到啦"); //測試使用,這裡寫業務邏輯

return "0";

}

}

GET方法

@GET

@Path(value="/getMail")

@Consumes("*/*")

public String getMail(@PathParam("appID")String appID) {

//業務邏輯

return null;

}

4.web.xml

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

<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>WEB-INF/beans.xml</param-value>

</context-param>

<listener>

<!--spring管理-->

<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>

<servlet-mapping>

<servlet-name>cxf</servlet-name>

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

</servlet-mapping>

</web-app>

5.beans.xml //跟web.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:jaxrs="http://cxf.apache.org/jaxrs"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:security="http://www.springframework.org/schema/security"

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://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="Service" class="com.service.impl.ServiceImpl" />

  <jaxrs:server id="restfulService" address="/public">

    <jaxrs:serviceBeans>

      <ref bean="Service"/>

    </jaxrs:serviceBeans>

  </jaxrs:server>

</beans>

6.打包部署

7.浏覽器輸入

http://localhost:8080/Test //Test輸入工程名

顯示

CXF架構的restful webservice 環境配置(一) 服務端

點開後看到自己釋出的2個接口

CXF架構的restful webservice 環境配置(一) 服務端

繼續閱讀