WebService

服务端

配置文件

web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!-- spring配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- spring核心监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- webservice初始化代码 -->
<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
<!-- 扫描 @Server @Controller @Repository -->
<context:component-scan base-package="com.yuda"/>

<!-- 加载properties文件 -->
<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}"/>
<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="1"/>
<!--连接池中保留的最小连接数。 -->
<property name="minPoolSize" value="1"/>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="100"/>
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="60"/>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="5"/>
<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="60"/>
</bean>
<!-- spring 整合 JPA配置 -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!-- 连接池 -->
<property name="dataSource" ref="crmdataSource"/>
<!-- 加载jpa注解类 -->
<property name="packagesToScan">
<list>
<value>com.yuda.crm.domain</value>
</list>
</property>
<!-- JPA方言 如果配置了jpaVendorAdapter,这项可以不配置 <property name="jpaDialect"> <bean
class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /> </property> -->
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<!-- 数据库类型 -->
<property name="database" value="ORACLE"/>
<!-- hibernate方言 -->
<property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect"/>
<!-- 自动建表 -->
<property name="generateDdl" value="true"/>
<!-- 控制台打印SQL语句 -->
<property name="showSql" value="true"/>
</bean>
</property>
<!-- jpa使用hibernate实现 -->
<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">
<!--注册webservice-->
<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(){...})
    //或者form表单传递到后台的数据,[key(input里的name)-value(input里的value)]

    发布为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
//接口需要有具体实现,并且实现类使用@Service声明为一个bean
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);

// 将客户关联到定区上 , 将所有客户id 拼成字符串 1,2,3
@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

//Constant.CRM_HTTPURL=="http://localhost:9002/crm_management/services/customerService/"
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();