webservice+restful+cxf(服务端客户端调用)

1.web.xml

<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>/ws/*</url-pattern>
</servlet-mapping>

 2.rest.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:cxf="http://cxf.apache.org/core"
 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
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
 <import resource="classpath:META-INF/cxf/cxf.xml" />
 <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />
 <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
 <jaxrs:server id="restServiceContainer" address="/rest">
  <jaxrs:serviceBeans>
   <ref bean="restSample" />
   <ref bean="restVuln" />
  </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>

3.java

@Path(value = "/sample")
public interface RESTSample {
 @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Path("/test/{param}")
    public String doRequestTest(@PathParam("param") String param, 
            @Context HttpServletRequest servletRequest, @Context HttpServletResponse servletResponse);

4.直接访问路径  其中ws是web.xml中的配置,rest是rest.xml文件中的配置,demo是项目path路径

(1)直接显示发布接口

http://192.168.28.101:8080/scanner7.0/ws

webservice+restful+cxf(服务端客户端调用)

(2)直接访问资源,直接得到  demo资源下list的数据并且用xml格式返回

http://192.168.28.101:8080/scanner7.0/ws/rest/demo/list?_type=xml //同样可以使用_type=json返回数据

webservice+restful+cxf(服务端客户端调用)

5.上边都是服务端发布,接下来是客户端调用

 private static WebClient client;
    
    @Before
    public void init() {
        // 手动创建webClient对象,注意这里的地址是发布的那个/rest地址
        String url = "http://192.168.28.101:8080/scanner/ws/rest/";
        client = WebClient.create(url);
        // 从Spring Ioc容器中拿webClient对象
//        ApplicationContext ctx = new ClassPathXmlApplicationContext("WEB-INF/applicationContext-client.xml");
//        client = (WebClient) ctx.getBean("webClient", WebClient.class);
    }
    @After
    public void destory(){
    }
   
    
    //自己�??发的
    @Test
    public void testGet() {
     try {
        System.out.println(client.path("sample").accept(MediaType.TEXT_PLAIN).get(String.class));
     } catch (Exception e) {
      e.printStackTrace();
     }
    }
    
    @Test
    public void testRequest() {
        System.out.println(client.path("sample/request/234234").accept(MediaType.TEXT_PLAIN).get(String.class));
    }
    
    @Test
    public void testBean() {
        User user = client.path("sample/bean/{id}", 25).accept(MediaType.APPLICATION_XML).get(User.class);
        System.out.println(user);
    }
    
    @Test
    public void testList() {
        System.out.println("--------------"+client.path("sample/list").accept(MediaType.APPLICATION_XML).get(Users.class).getUsers());
    }

6.针对返回json数据显示格式调整

<bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
<property name="serializeAsArray" value="true" />
<property name="arrayKeys" ref="jsonKeys" />
<property name="produceMediaTypes" ref="jsonTypes" />
<property name="consumeMediaTypes" ref="jsonTypes" />
<property name="ignoreNamespaces" value="true" />
<property name="dropRootElement" value="true" />
<property name="ignoreMixedContent" value="true" />
<property name="supportUnwrapped" value="true" />
<property name="attributesToElements" value="true" />
</bean>

7.针对restful拦截器使用

<jaxrs:server id="rest_HelloWorld" address="/">  
        <jaxrs:inInterceptors>  
           <ref bean="inMessageInterceptor"/>  
        </jaxrs:inInterceptors>  
        <jaxrs:outInterceptors>  
            <ref bean="outMessageInterceptor"/>  
        </jaxrs:outInterceptors>  
        <jaxrs:serviceBeans>  
            <ref bean="rest_HelloWorldImpl" />  
        </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>

大工搞成,下边可以下载

链接:http://pan.baidu.com/s/1i3LdDGD 密码:4rqe

你可能感兴趣的:(webservice+restful+cxf(服务端客户端调用))