Java Web Service-CXF
-基于Restful的Web服务
2015年11月13日
//web.xml
<?xmlversion="1.0" encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"id="WebApp_ID" version="3.1">
<display-name>CXFDemo</display-name>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
//applicationContext.xml
<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrshttp://cxf.apache.org/schemas/jaxrs.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<importresource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<importresource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxrs:serverid="rs1" address="/rs">
<jaxrs:serviceBeans>
<beanclass="lee.HelloWorldImpl" />
</jaxrs:serviceBeans>
</jaxrs:server>
</beans>
//IHelloWorld.java
package lee;
public interface IHelloWorldextends java.rmi.Remote {
public java.lang.String sayHello();
public java.lang.String say(String arg0);
}
//HelloWorldImpl.java
package lee;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
@Path("/")
public class HelloWorldImplimplements IHelloWorld {
@Override
@GET
@Path("/say/{msg}")
public String say(@PathParam("msg")String msg) {
// TODO Auto-generated method stub
return "Hello"+msg;
}
@Override
@GET
@Path("/sayhi")
public String sayHello() {
// TODO Auto-generated method stub
return "Hello";
}
}
http://192.168.41.131:8080/CXFDemo/services/rs/sayhi
http://192.168.41.131:8080/CXFDemo/services/rs/say/xx
将所有不同表现形式的资源、服务抽象为资源并分配URI。
简化Web服务协议(SOAP协议太过复杂,且只支持POST,无法使用GET获取)。
使用注释标记各种服务属性。
HTTP请求方式:@PUT,@GET,@POST,@DELETE。
HTTP请求的MIME类型:@Consumers
HTTP返回的MIME类型:@Produces
HTTP请求参数来源:@PathParam,@QueryParam,@HeaderParam,@CookieParam,@MatrixParam,@FormParam。
HTTP请求参数默认值:@DefaultValue。
参考:http://developer.51cto.com/art/200908/141825.htm
http://baike.baidu.com/link?url=Z5A9ifUJH9GLwJR_JwjbhOUW2LUpOImOvB9hIosMwojDmKWQ7-eQmio6kOIVXAB59We2xJFdvhPSKxZP9hRHWq
CXF实现JAX-RS。
//web.xml
<?xmlversion="1.0" encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"id="WebApp_ID" version="3.1">
<display-name>CXFClientDemo</display-name>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>h</servlet-name>
<servlet-class>
lee.Client
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>h</servlet-name>
<url-pattern>/client/h</url-pattern>
</servlet-mapping>
</web-app>
//applicationContext.xml
<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
//Clent.java
package lee;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.apache.cxf.jaxrs.client.WebClient;
import org.springframework.web.context.WebApplicationContext;
importorg.springframework.web.context.support.WebApplicationContextUtils;
public class Client extendsHttpServlet {
public void service(HttpServletRequest reqest,HttpServletResponse response) {
WebApplicationContext ctx =WebApplicationContextUtils.getWebApplicationContext(getServletContext());
WebClientclient=WebClient.create("http://192.168.41.131:8080/CXFDemo/services/rs/whosay/xx?msg=yy");
String strResp=(String)client.accept("text/plain").get(String.class);
System.out.println(strResp);
}
}
Client是Web服务的代理,指定服务的地址,然后使用get()得到服务的结果,转化为指定的结果类型。
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrshttp://cxf.apache.org/schemas/jaxrs.xsd">
<jaxrs:clientid="webClient"address="http://192.168.41.131:8080/CXFDemo/services/rs/whosay/xx?msg=yy"
serviceClass="org.apache.cxf.jaxrs.client.WebClient">
<jaxrs:headers>
<entrykey="Accept" value="text/plain" />
</jaxrs:headers>
</jaxrs:client>
WebClientclient=ctx.getBean("webClient",WebClient.class);
String strResp=client.get(String.class);
WebClientclient=WebClient.create("http://192.168.41.131:8080/CXFDemo/services/rs/whosay/xx?msg=yy");
StringstrResp=(String)client.accept("text/plain").get(String.class);
参考:http://cxf.apache.org/docs/jax-rs-client-api.html
//web.xml
<?xmlversion="1.0" encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"id="WebApp_ID" version="3.1">
<display-name>CXFClientDemo</display-name>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>h</servlet-name>
<servlet-class>
lee.Client
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>h</servlet-name>
<url-pattern>/client/h</url-pattern>
</servlet-mapping>
</web-app>
//applicationContext.xml
<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrshttp://cxf.apache.org/schemas/jaxrs.xsd">
<jaxrs:clientid="webClient"address="http://192.168.41.131:8080/CXFDemo/services/rs/whosay/xx?msg=yy"
serviceClass="org.apache.cxf.jaxrs.client.WebClient">
<jaxrs:headers>
<entrykey="Accept" value="text/plain" />
</jaxrs:headers>
</jaxrs:client>
</beans>
//Clent.java
package lee;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
import org.apache.cxf.jaxrs.client.WebClient;
importorg.springframework.web.context.WebApplicationContext;
importorg.springframework.web.context.support.WebApplicationContextUtils;
public class Client extendsHttpServlet {
public void service(HttpServletRequest reqest,HttpServletResponse response) {
WebApplicationContext ctx =WebApplicationContextUtils.getWebApplicationContext(getServletContext());
WebClientclient=ctx.getBean("webClient",WebClient.class);
StringstrResp=client.get(String.class);
System.out.println(strResp);
}
}