一、寫在前面
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; public User(){ } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User [name=" + name + ", sex=" + sex + ", age=" + age + "]"; } }
新建對應接口和繼承接口

對應代碼
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="/getString") public String getString() throws Exception; @POST @Path(value="/getUserName") public User getUser() throws Exception; }
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 { public final static String APPLICATION_JSON_UTF_8 = "application/json; charset=UTF-8"; public final static String APPLICATION_XML_UTF_8 = "application/xml; charset=UTF-8"; }
接口定義完成,下面創建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 public String getString() throws Exception { return "this is ws test"; } @Override public User getUser() throws Exception { User user=new User(); user.setAge(13); user.setName("minzhou"); user.setSex("man"); return 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> <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> <!-- CXF Servlet --> <servlet> <servlet-name>CXFService</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFService</servlet-name> <url-pattern>/rs/*</url-pattern> </servlet-mapping> <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"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <jaxrs:server id="rest-container" address="/"> <jaxrs:serviceBeans> <ref bean="testFacade" /> </jaxrs:serviceBeans> </jaxrs:server> </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 "> <context:annotation-config /> <aop:aspectj-autoproxy/> <context:component-scan base-package="com" /> </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"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <jaxrs:server id="rest-container" address="/"> <jaxrs:providers> <bean class="com.test.interfacesImpl.ISSJacksonJaxbJsonProvider"> <constructor-arg type="com.fasterxml.jackson.databind.ObjectMapper"> <bean class="com.fasterxml.jackson.databind.ObjectMapper"> <property name="serializationInclusion" value="NON_NULL" /> </bean> </constructor-arg> </bean> </jaxrs:providers> <jaxrs:serviceBeans> <ref bean="testFacade" /> </jaxrs:serviceBeans> </jaxrs:server> </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 "> <http-conf:conduit name="{WSDL Namespace}portName.http-conduit"> <http-conf:client ConnectionTimeout="30000" ReceiveTimeout="30000" /> </http-conf:conduit> <cxf:bus> <cxf:features> <cxf:logging /> <!--<cxf:fastinfoset force="false" /> --> </cxf:features> <!-- compress the exchange data size --> <cxf:inInterceptors> <bean class="org.apache.cxf.transport.common.gzip.GZIPInInterceptor" /> </cxf:inInterceptors> <cxf:outInterceptors> <bean class="org.apache.cxf.transport.common.gzip.GZIPOutInterceptor" /> </cxf:outInterceptors> </cxf:bus> <jaxrs:client id="testfacade" address="${dataserver.rs.address}" serviceClass="com.test.interfaces.IuserFacade" inheritHeaders="true"> <jaxrs:headers> <entry key="Accept" value="application/json" /> <entry key="Content-Type" value="application/json;charset=UTF-8" /> <entry key="Authorization" value="Basic dG9tOjEyMzQ1Njc4" /> </jaxrs:headers> <jaxrs:providers> <bean class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider" /> </jaxrs:providers> </jaxrs:client> </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> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:conf/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/dispatcher/*</url-pattern> </servlet-mapping> <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"> <context:annotation-config /> <aop:aspectj-autoproxy proxy-target-class="true" /> <context:component-scan base-package="com.test" /> <util:list id="messageConverters"> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /> </util:list> <!-- content neotiating view resolver, a delegate over the accept header --> <bean id="contentNegotiatingViewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="contentNegotiationManager"> <bean class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <property name="mediaTypes"> <props> <prop key="atom">application/atom+xml;charset=UTF-8</prop> <prop key="xml">application/xml;charset=UTF-8</prop> <prop key="html">text/html;charset=UTF-8</prop> <prop key="json">application/json;charset=UTF-8</prop> </props> </property> </bean> </property> <property name="defaultViews"> <list> <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"> </bean> </list> </property> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/page/" /> <property name="suffix" value=".jsp" /> </bean> </list> </property> </bean> <bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <property name="providerClass" value="org.hibernate.validator.HibernateValidator" /> </bean> </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 "> <context:component-scan base-package="com" /> <aop:aspectj-autoproxy/> <context:property-placeholder location="classpath:conf/DEV_dataserver.properties" ignore-unresolvable="true"/> <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、對應代碼下載http://files.cnblogs.com/files/minzhousblogs/TestWebServise001.zip 里面沒有jar包,可以直接放jar
