基于cxf开发restful风格的Web Service

一、写在前面

  webservice一些简单的其他用法和概念,就不在这里赘述了,相信大家都可以在网上查到,我也是一个新手,写这篇文章的目的一方面是想记录自己成长的历程,另一方面是因为学习这个的时候花了点时间,希望本文章能为大家节约点时间。当然描述的可能不到位,望谅解。

二、创建项目

2.1、创建公用接口project

  为了方便服务端和客户端调用接口可以先创建一个接口java project,单独建一个接口project的用处后面再说。

然后导入对应jar包,可以去cxf官网下载http://cxf.apache.org/download.html把里面的所有包可以都导入

新建一个实体类User

定义实体类

复制代码
package com.test.entity;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class User {
private String name;
private String sex;
private Integer age;

</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> User(){
    
}
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getName() {
    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> name;
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setName(String name) {
    </span><span style="color: #0000ff;">this</span>.name =<span style="color: #000000;"> name;
}
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getSex() {
    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> sex;
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setSex(String sex) {
    </span><span style="color: #0000ff;">this</span>.sex =<span style="color: #000000;"> sex;
}
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Integer getAge() {
    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> age;
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setAge(Integer age) {
    </span><span style="color: #0000ff;">this</span>.age =<span style="color: #000000;"> age;
}
@Override
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String toString() {
    </span><span style="color: #0000ff;">return</span> "User [name=" + name + ", sex=" + sex + ", age=" + age + "]"<span style="color: #000000;">;
}

}

复制代码

新建对应接口和继承接口

对应代码

复制代码
package com.test.interfaces;

import javax.ws.rs.POST;
import javax.ws.rs.Path;

import com.test.entity.User;

@Path(value="/user")
public interface IuserFacade extends CommonFacade{
public final static String FACADE_NAME = "testFacade";

        @POST
        @Path(value</span>="/getString"<span style="color: #000000;">)
        </span><span style="color: #0000ff;">public</span> String getString() <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception;
        
        @POST
        @Path(value</span>="/getUserName"<span style="color: #000000;">)
        </span><span style="color: #0000ff;">public</span> User getUser() <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception;
        

}</span></pre>
复制代码
复制代码
package com.test.interfaces;

import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;

@Consumes(value = { CommonFacade.APPLICATION_JSON_UTF_8, CommonFacade.APPLICATION_XML_UTF_8 })
@Produces(value
= { CommonFacade.APPLICATION_JSON_UTF_8, CommonFacade.APPLICATION_XML_UTF_8 })
public interface CommonFacade {

</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">final</span> <span style="color: #0000ff;">static</span> String APPLICATION_JSON_UTF_8 = "application/json; charset=UTF-8"<span style="color: #000000;">;

</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">final</span> <span style="color: #0000ff;">static</span> String APPLICATION_XML_UTF_8 = "application/xml; charset=UTF-8"<span style="color: #000000;">;

}

复制代码

接口定义完成,下面创建web service。

2.2、创建实现接口web service

  这里创建的普通的web project,实现web service主要是看配置文件。

因为这里的需要实现上面建的接口,所以先关联Test-interfacse

 

这样在实现的时候才可以用不然会提示不存在

实现类为

复制代码
package com.test.interfacesImpl;

import org.springframework.stereotype.Service;

import com.test.entity.User;
import com.test.interfaces.IuserFacade;

@Service(value = IuserFacade.FACADE_NAME)
public class IuserFacadeImpl implements IuserFacade{

@Override
</span><span style="color: #0000ff;">public</span> String getString() <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception {
    
    </span><span style="color: #0000ff;">return</span> "this is ws test"<span style="color: #000000;">;
}

@Override
</span><span style="color: #0000ff;">public</span> User getUser() <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception {
    User user</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> User();
    user.setAge(</span>13<span style="color: #000000;">);
    user.setName(</span>"minzhou"<span style="color: #000000;">);
    user.setSex(</span>"man"<span style="color: #000000;">);
    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> user;

}

}

复制代码

配置web.xml文件

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>    
   <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">context-param</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">param-name</span><span style="color: #0000ff;">&gt;</span>contextConfigLocation<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">param-name</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">param-value</span><span style="color: #0000ff;">&gt;</span>classpath*:conf/applicationContext*.xml<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">param-value</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">context-param</span><span style="color: #0000ff;">&gt;</span>

<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">listener</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">listener-class</span><span style="color: #0000ff;">&gt;</span>org.springframework.web.context.ContextLoaderListener<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">listener-class</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">listener</span><span style="color: #0000ff;">&gt;</span>

    <span style="color: #008000;">&lt;!--</span><span style="color: #008000;"> CXF Servlet </span><span style="color: #008000;">--&gt;</span>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">servlet</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">servlet-name</span><span style="color: #0000ff;">&gt;</span>CXFService<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">servlet-name</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">servlet-class</span><span style="color: #0000ff;">&gt;</span>org.apache.cxf.transport.servlet.CXFServlet<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">servlet-class</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">servlet</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">servlet-mapping</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">servlet-name</span><span style="color: #0000ff;">&gt;</span>CXFService<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">servlet-name</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">url-pattern</span><span style="color: #0000ff;">&gt;</span>/rs/*<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">url-pattern</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">servlet-mapping</span><span style="color: #0000ff;">&gt;</span>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

复制代码

创建conf ,在conf里面创建对应xml

对应applicationContext-cxf.xml和applicationContext-default.xml分别为

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:cxf="http://cxf.apache.org/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs" 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://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
        http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">import </span><span style="color: #ff0000;">resource</span><span style="color: #0000ff;">="classpath:META-INF/cxf/cxf.xml"</span> <span style="color: #0000ff;">/&gt;</span>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">import </span><span style="color: #ff0000;">resource</span><span style="color: #0000ff;">="classpath:META-INF/cxf/cxf-servlet.xml"</span> <span style="color: #0000ff;">/&gt;</span>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">import </span><span style="color: #ff0000;">resource</span><span style="color: #0000ff;">="classpath:META-INF/cxf/cxf-extension-soap.xml"</span> <span style="color: #0000ff;">/&gt;</span>



<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">jaxrs:server </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="rest-container"</span><span style="color: #ff0000;"> address</span><span style="color: #0000ff;">="/"</span><span style="color: #0000ff;">&gt;</span>
    
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">jaxrs:serviceBeans</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">ref </span><span style="color: #ff0000;">bean</span><span style="color: #0000ff;">="testFacade"</span> <span style="color: #0000ff;">/&gt;</span>

    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">jaxrs:serviceBeans</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">jaxrs:server</span><span style="color: #0000ff;">&gt;</span>

</beans>

复制代码
复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    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://www.springframework.org/schema/aop  
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        ">
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">context:annotation-config </span><span style="color: #0000ff;">/&gt;</span>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">aop:aspectj-autoproxy</span><span style="color: #0000ff;">/&gt;</span>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">context:component-scan </span><span style="color: #ff0000;">base-package</span><span style="color: #0000ff;">="com"</span> <span style="color: #0000ff;">/&gt;</span>

</beans>

复制代码

运行后输入http://localhost:8080/Test-WS/rs/?_wadl得到

这里web service端就完成了,通过把地址发给别人就可以访问了,这里面的接口project就可以同时打包发过去,调用的时候就可以引用接口jar包,和地址,访问了。

当然到这里还存在问题,在客户端调用的时候就会发现问题,有兴趣的可以先把这放着,直接写客户端代码,然后调用,看看是什么问题。为了避免问题我先把代码加上

新加一个类

复制代码
package com.test.interfacesImpl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;

public class ISSJacksonJaxbJsonProvider extends JacksonJaxbJsonProvider {
public ISSJacksonJaxbJsonProvider(ObjectMapper objectMapper){
super(objectMapper, DEFAULT_ANNOTATIONS);
}
}

复制代码

修改applicationContext-cxf.xml 增加<jaxrs:providers></jaxrs:providers>引用新建的类

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:cxf="http://cxf.apache.org/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs" 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://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
        http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">import </span><span style="color: #ff0000;">resource</span><span style="color: #0000ff;">="classpath:META-INF/cxf/cxf.xml"</span> <span style="color: #0000ff;">/&gt;</span>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">import </span><span style="color: #ff0000;">resource</span><span style="color: #0000ff;">="classpath:META-INF/cxf/cxf-servlet.xml"</span> <span style="color: #0000ff;">/&gt;</span>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">import </span><span style="color: #ff0000;">resource</span><span style="color: #0000ff;">="classpath:META-INF/cxf/cxf-extension-soap.xml"</span> <span style="color: #0000ff;">/&gt;</span>



<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">jaxrs:server </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="rest-container"</span><span style="color: #ff0000;"> address</span><span style="color: #0000ff;">="/"</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">jaxrs:providers</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">bean </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="com.test.interfacesImpl.ISSJacksonJaxbJsonProvider"</span><span style="color: #0000ff;">&gt;</span>
            <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">constructor-arg </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="com.fasterxml.jackson.databind.ObjectMapper"</span><span style="color: #0000ff;">&gt;</span>
                <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">bean </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="com.fasterxml.jackson.databind.ObjectMapper"</span><span style="color: #0000ff;">&gt;</span>
                    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="serializationInclusion"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="NON_NULL"</span> <span style="color: #0000ff;">/&gt;</span>
                <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">bean</span><span style="color: #0000ff;">&gt;</span>
            <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">constructor-arg</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">bean</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">jaxrs:providers</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">jaxrs:serviceBeans</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">ref </span><span style="color: #ff0000;">bean</span><span style="color: #0000ff;">="testFacade"</span> <span style="color: #0000ff;">/&gt;</span>

    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">jaxrs:serviceBeans</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">jaxrs:server</span><span style="color: #0000ff;">&gt;</span>

</beans>

复制代码

三、客户端实现web service

  创建一个web project Test-Client同理关联接口,当然也可以不关联接口,直接把接口所导成的jar包导入即可。因为是本地项目所以可以这样,若是给人调用,只需要把web service接口地址和打包的接口jar给人就可以。

创建调用外部webservice控制器

 

testcontroller.java代码

复制代码
package com.test.conroller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.test.entity.User;
import com.test.interfaces.IuserFacade;

@Controller
@RequestMapping("/test")
public class TestController {
@Resource(name = "testfacade")
private IuserFacade testfacae;

@RequestMapping("/getString")
public @ResponseBody
void testGetString()throws Exception {
    String tests=testfacae.getString();
    System.out.println(tests);
}

@RequestMapping("/getUser")
public  @ResponseBody
String testGetUser()throws Exception{
    
    User users=testfacae.getUser();
    System.out.println(users.toString());
    
    System.out.println("my name is:"+users.getName()+" and my age is "+users.getAge());
    return users.toString();
    
/*    UserQueryResp uq=userFacade.getUserQueryRespById("40289518501706fe01504a91cc2c00d8");*/
    

    
}

}

复制代码

这里@Resource(name =  "testfacade")需要在xml中配置

具体applicationContext-cxf.xml代码

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
    xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd   http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/transports/http/configuration   http://cxf.apache.org/schemas/configuration/http-conf.xsd
http://cxf.apache.org/jaxrs   http://cxf.apache.org/schemas/jaxrs.xsd
http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context.xsd ">
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">http-conf:conduit </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="{WSDL Namespace}portName.http-conduit"</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">http-conf:client </span><span style="color: #ff0000;">ConnectionTimeout</span><span style="color: #0000ff;">="30000"</span><span style="color: #ff0000;">
        ReceiveTimeout</span><span style="color: #0000ff;">="30000"</span> <span style="color: #0000ff;">/&gt;</span>
<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">http-conf:conduit</span><span style="color: #0000ff;">&gt;</span>

<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">cxf:bus</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">cxf:features</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">cxf:logging </span><span style="color: #0000ff;">/&gt;</span>
        <span style="color: #008000;">&lt;!--</span><span style="color: #008000;">&lt;cxf:fastinfoset force="false" /&gt; </span><span style="color: #008000;">--&gt;</span>
    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">cxf:features</span><span style="color: #0000ff;">&gt;</span>

    <span style="color: #008000;">&lt;!--</span><span style="color: #008000;"> compress the exchange data size </span><span style="color: #008000;">--&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">cxf:inInterceptors</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">bean </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="org.apache.cxf.transport.common.gzip.GZIPInInterceptor"</span> <span style="color: #0000ff;">/&gt;</span>
    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">cxf:inInterceptors</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">cxf:outInterceptors</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">bean </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="org.apache.cxf.transport.common.gzip.GZIPOutInterceptor"</span> <span style="color: #0000ff;">/&gt;</span>
    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">cxf:outInterceptors</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">cxf:bus</span><span style="color: #0000ff;">&gt;</span>


<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">jaxrs:client </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="testfacade"</span><span style="color: #ff0000;"> address</span><span style="color: #0000ff;">="${dataserver.rs.address}"</span><span style="color: #ff0000;">
    serviceClass</span><span style="color: #0000ff;">="com.test.interfaces.IuserFacade"</span><span style="color: #ff0000;">
    inheritHeaders</span><span style="color: #0000ff;">="true"</span><span style="color: #0000ff;">&gt;</span>

    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">jaxrs:headers</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">entry </span><span style="color: #ff0000;">key</span><span style="color: #0000ff;">="Accept"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="application/json"</span> <span style="color: #0000ff;">/&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">entry </span><span style="color: #ff0000;">key</span><span style="color: #0000ff;">="Content-Type"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="application/json;charset=UTF-8"</span> <span style="color: #0000ff;">/&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">entry </span><span style="color: #ff0000;">key</span><span style="color: #0000ff;">="Authorization"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="Basic dG9tOjEyMzQ1Njc4"</span> <span style="color: #0000ff;">/&gt;</span>
    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">jaxrs:headers</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">jaxrs:providers</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">bean </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider"</span> <span style="color: #0000ff;">/&gt;</span>
    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">jaxrs:providers</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">jaxrs:client</span><span style="color: #0000ff;">&gt;</span>

</beans>

复制代码

其中用到${dataserver.rs.address}这里配置到了DEV_dataserver.properties

dataserver.rs.address=http://127.0.0.1:8080/Test-WS/rs/?_wadl

web.xml配置

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>    

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">servlet</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">servlet-name</span><span style="color: #0000ff;">&gt;</span>DispatcherServlet<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">servlet-name</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">servlet-class</span><span style="color: #0000ff;">&gt;</span>org.springframework.web.servlet.DispatcherServlet<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">servlet-class</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">init-param</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">param-name</span><span style="color: #0000ff;">&gt;</span>contextConfigLocation<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">param-name</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">param-value</span><span style="color: #0000ff;">&gt;</span>classpath*:conf/dispatcher-servlet.xml<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">param-value</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">init-param</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">load-on-startup</span><span style="color: #0000ff;">&gt;</span>1<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">load-on-startup</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">servlet</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">servlet-mapping</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">servlet-name</span><span style="color: #0000ff;">&gt;</span>DispatcherServlet<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">servlet-name</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">url-pattern</span><span style="color: #0000ff;">&gt;</span>/dispatcher/*<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">url-pattern</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">servlet-mapping</span><span style="color: #0000ff;">&gt;</span>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

复制代码

dispatcher-servlet.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:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">context:annotation-config </span><span style="color: #0000ff;">/&gt;</span>

<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">aop:aspectj-autoproxy </span><span style="color: #ff0000;">proxy-target-class</span><span style="color: #0000ff;">="true"</span> <span style="color: #0000ff;">/&gt;</span>

<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">context:component-scan </span><span style="color: #ff0000;">base-package</span><span style="color: #0000ff;">="com.test"</span> <span style="color: #0000ff;">/&gt;</span>

<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">util:list </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="messageConverters"</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">bean </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"</span> <span style="color: #0000ff;">/&gt;</span>
<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">util:list</span><span style="color: #0000ff;">&gt;</span>



<span style="color: #008000;">&lt;!--</span><span style="color: #008000;"> content neotiating view resolver, a delegate over the accept header </span><span style="color: #008000;">--&gt;</span>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">bean </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="contentNegotiatingViewResolver"</span><span style="color: #ff0000;">
    class</span><span style="color: #0000ff;">="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="contentNegotiationManager"</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">bean
            </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"</span><span style="color: #0000ff;">&gt;</span>
            <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="mediaTypes"</span><span style="color: #0000ff;">&gt;</span>
                <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">props</span><span style="color: #0000ff;">&gt;</span>
                    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">prop </span><span style="color: #ff0000;">key</span><span style="color: #0000ff;">="atom"</span><span style="color: #0000ff;">&gt;</span>application/atom+xml;charset=UTF-8<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">prop</span><span style="color: #0000ff;">&gt;</span>
                    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">prop </span><span style="color: #ff0000;">key</span><span style="color: #0000ff;">="xml"</span><span style="color: #0000ff;">&gt;</span>application/xml;charset=UTF-8<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">prop</span><span style="color: #0000ff;">&gt;</span>
                    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">prop </span><span style="color: #ff0000;">key</span><span style="color: #0000ff;">="html"</span><span style="color: #0000ff;">&gt;</span>text/html;charset=UTF-8<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">prop</span><span style="color: #0000ff;">&gt;</span>
                    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">prop </span><span style="color: #ff0000;">key</span><span style="color: #0000ff;">="json"</span><span style="color: #0000ff;">&gt;</span>application/json;charset=UTF-8<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">prop</span><span style="color: #0000ff;">&gt;</span>
                <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">props</span><span style="color: #0000ff;">&gt;</span>
            <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">property</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">bean</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">property</span><span style="color: #0000ff;">&gt;</span>

    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="defaultViews"</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">list</span><span style="color: #0000ff;">&gt;</span>
            <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">bean
                </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="org.springframework.web.servlet.view.json.MappingJackson2JsonView"</span><span style="color: #0000ff;">&gt;</span>
            <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">bean</span><span style="color: #0000ff;">&gt;</span>

        <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">list</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">property</span><span style="color: #0000ff;">&gt;</span>

    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="viewResolvers"</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">list</span><span style="color: #0000ff;">&gt;</span>
            <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">bean </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="org.springframework.web.servlet.view.BeanNameViewResolver"</span> <span style="color: #0000ff;">/&gt;</span>
            <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">bean
                </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="org.springframework.web.servlet.view.InternalResourceViewResolver"</span><span style="color: #0000ff;">&gt;</span>
                <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="viewClass"</span><span style="color: #ff0000;">
                    value</span><span style="color: #0000ff;">="org.springframework.web.servlet.view.JstlView"</span> <span style="color: #0000ff;">/&gt;</span>
                <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="prefix"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="/page/"</span> <span style="color: #0000ff;">/&gt;</span>
                <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="suffix"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">=".jsp"</span> <span style="color: #0000ff;">/&gt;</span>
            <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">bean</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">list</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">property</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">bean</span><span style="color: #0000ff;">&gt;</span>


<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">bean
    </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">property </span><span style="color: #ff0000;">name</span><span style="color: #0000ff;">="providerClass"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="org.hibernate.validator.HibernateValidator"</span> <span style="color: #0000ff;">/&gt;</span>
<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">bean</span><span style="color: #0000ff;">&gt;</span>

</beans>

复制代码

applicationContext-default.xml配置

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
    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/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop  
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">context:component-scan </span><span style="color: #ff0000;">base-package</span><span style="color: #0000ff;">="com"</span> <span style="color: #0000ff;">/&gt;</span>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">aop:aspectj-autoproxy</span><span style="color: #0000ff;">/&gt;</span>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">context:property-placeholder </span><span style="color: #ff0000;">location</span><span style="color: #0000ff;">="classpath:conf/DEV_dataserver.properties"</span><span style="color: #ff0000;"> ignore-unresolvable</span><span style="color: #0000ff;">="true"</span><span style="color: #0000ff;">/&gt;</span>

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html; charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html; charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
</beans>

复制代码

这样启动项目后可以访问http://localhost:8080/Test-Client/dispatcher/test/getUser得到,这样通过客户端访问服务端就可以得到相应的值,这里面的值比较简单,若是webservice端链接上数据库,就可以取得对应数据,客户端也可以在对应页面上获取值,本文只是写对应过程。当然里面的配置文件可能会有多余的,但是大致思想是这样的。

总结:1、通过创建web service 暴露对应访问地址,可以让开发人员调用而保护数据。

   2、单独创建接口project可以避免在服务端和客户端同时编辑接口,而且给外部访问的时候可以通过导入jar包提供方便

    3、对应代码下载https://files.cnblogs.com/files/minzhousblogs/TestWebServise001.zip 里面没有jar包,可以直接放jar

posted @ 2017-10-23 13:19  星朝  阅读(518)  评论(0编辑  收藏  举报