略
服务端
配置文件
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<servlet> <servlet-name>CXFService</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFService</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
|
Spring配置:
1 2 3 4 5 6 7 8 9
| <context:component-scan base-package="com.yuda"/>
<context:property-placeholder location="classpath:config.properties"/>
<import resource="applicationContext-dataSource.xml"/> <import resource="applicationContext-webService.xml"/>
|
*-dataSource.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| <bean id="crmdataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="driverClass" value="${jdbc.driver}"/> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> <property name="initialPoolSize" value="1"/> <property name="minPoolSize" value="1"/> <property name="maxPoolSize" value="100"/> <property name="maxIdleTime" value="60"/> <property name="acquireIncrement" value="5"/> <property name="idleConnectionTestPeriod" value="60"/> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="crmdataSource"/> <property name="packagesToScan"> <list> <value>com.yuda.crm.domain</value> </list> </property>
<property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="database" value="ORACLE"/> <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect"/> <property name="generateDdl" value="true"/> <property name="showSql" value="true"/> </bean> </property> <property name="persistenceProvider"> <bean class="org.hibernate.ejb.HibernatePersistence"></bean> </property> </bean> <jpa:repositories base-package="com.yuda.crm.dao" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
|
*-webService.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <beans xmlns="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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> <jaxrs:server id="customerService" address="/customerService"> <jaxrs:serviceBeans> <bean class="com.yuda.crm.service.impl.CustomerServiceImpl"/> </jaxrs:serviceBeans> <jaxrs:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/> </jaxrs:inInterceptors> <jaxrs:outInterceptors> <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/> </jaxrs:outInterceptors> </jaxrs:server> </beans>
|
通过<context:component-scan base-package="com.yuda"/>
扫描com.yuda下的注解,其中的Service注解设置为WebService
Java代码
@GET
查询
@PUT
修改
@POST
增加
@DELECT
删除
@Path
区分哪个服务的路径
@Produces
生产(返回值)出的数据被转换为什么格式的数据
{ "application/xml", "application/json" }
表示可以转换为xml或者json.
@Consumes
消费(参数)由那种格式转换而来的.
{ "application/xml", "application/json" }
表示前台传来的数据可以是json或者xml
@PathParam("xxx")
设置在参数前面,与@Path
配合使用,@Path
中使用EL表达式,例如{xxx}.表示传来的数据是什么.例如:
1 2 3 4 5 6 7 8 9
| @Path("/test/{year}/{month}/{day}") @GET @Consumes({"application/xml", "application/json"}) void historyToday( @PathParam("year") String year, @PathParam("month") String month, @PathParam("day") String day){ sout(year+monty+day); }
|
@QueryParam("xxx")
设置在参数前面,表示该URL传递的非get方式传递的数据,例如:
1 2
| $.post("url",{json格式数据},funcation(){...})
|
发布为webService后,访问http://XXX:8080/service/customerService/test/2017/1/1
即可在控制台打印年月日
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| public interface CustomerService {
@Path("/noassociationcustomers") @GET @Produces({ "application/xml", "application/json" }) List<Customer> findNoAssociationCustomers();
@Path("/associationfixedareacustomers/{fixedareaid}") @GET @Produces({ "application/xml", "application/json" }) List<Customer> findHasAssociationFixedAreaCustomers( @PathParam("fixedareaid") String fixedAreaId);
@Path("/associationcustomerstofixedarea") @PUT void associationCustomersToFixedArea( @QueryParam("customerIdStr") String customerIdStr, @QueryParam("fixedAreaId") String fixedAreaId);
@Path("/customer") @POST @Consumes({"application/xml", "application/json"}) void regist(Customer customer);
@Path("/customer/telephone/{telephone}") @GET @Consumes({"application/xml", "application/json"}) Customer findByTelePhone(@PathParam("telephone") String telephone);
@Path("/customer/updatetype/{telephone}") @GET void updateType(@PathParam("telephone") String telephone);
}
|
客户端(要与服务端对应)
- create()方法,指定请求的url(WebService的网络地址)
- accept(..)方法,指定返回的数据格式,是xml还是json
- getCollection(..)方法,如果webService返回的是个List集合,就使用这个方法指定List集合内的数据的类型.
- type(..),指定上传数据时,使用了什么格式的数据
- post(..),使用的请求方式为post
- put(),使用put请求方式.
- get(),使用get请求方式
- delect(),使用delect请求方式
注意: 拼接的URL必须是服务端提供给的,否则找不到该服务(就是找资源要找对车牌),并且要注意服务需要什么参数和该服务有什么返回值或没有返回值,最后说一句,WebClient来自与cxf-rt-rs-client-X.X.X.jar
实例代码:
客户端使用WebService的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
Collection<? extends Customer> collection = WebClient.create(Constant.CRM_HTTPURL + "noassociationcustomers") .accept(MediaType.APPLICATION_JSON) .getCollection(Customer.class);
WebClient.create("http://localhost:9002/crm_management/services/customerService/customer") .type(MediaType.APPLICATION_JSON).post(customer);
String url = Constant.CRM_HTTPURL + "associationcustomerstofixedarea?customerIdStr=" + customers + "&fixedAreaId=" + fixedArea.getId(); System.out.println("URL==" + url); WebClient.create(url) .put(null)
WebClient.create("http://localhost:9002/crm_management/services/customerService/customer/updatetype/" + customer.getTelephone()).get();
|