CRM專案基於CXF-RS釋出WebService服務
阿新 • • 發佈:2018-12-31
最近做BOS物流管理系統的時候,需要CRM專案提供WebService
CRM專案所用技術為:
- spring+struct2+springData(hibernate整合)
- 分散式技術為CXF-RS 實現多系統之間的通訊
CRM專案的初始化
因為BOS專案與CRM專案都需要用到Customer實體類,因此將CRM_domain獨立出來
pom檔案的依賴
<dependency>
<!--CXF-rs依賴包-->
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency>
<!--CXF-rs客戶端-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
<version> ${cxf.version}</version>
</dependency>
<!--CXF-rs擴充套件json提供者-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>${cxf.version}</version>
</dependency>
<! --轉換json依賴-->
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.3.7</version>
</dependency>
CRM實體類的配置
@Entity
@Table(name = "T_CUSTOMER")
@XmlRootElement(name = "customer")
public class Customer {
@Id
@GeneratedValue()
@Column(name = "C_ID")
private Integer id; // 主鍵id
@Column(name = "C_USERNAME")
private String username; // 使用者名稱
@Column(name = "C_PASSWORD")
private String password; // 密碼
@Column(name = "C_TYPE")
private Integer type; // 型別 設定1 繫結郵箱
@Column(name = "C_BRITHDAY")
@Temporal(TemporalType.DATE)
private Date birthday; // 生日
@Column(name = "C_SEX")
private Integer sex; // 性別 1男 2女
@Column(name = "C_TELEPHONE")
private String telephone; // 手機
@Column(name = "C_COMPANY")
private String company; // 公司
@Column(name = "C_DEPARTMENT")
private String department; // 部門
@Column(name = "C_POSITION")
private String position; // 職位
@Column(name = "C_ADDRESS")
private String address; // 地址
@Column(name = "C_MOBILEPHONE")
private String mobilePhone; // 座機
@Column(name = "C_EMAIL")
private String email; // 郵箱
@Column(name = "C_Fixed_AREA_ID")
private String fixedAreaId; // 定區編碼
CRM服務類的配置
- web.xml的配置
<!-- 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>
- applicationContext的配置
資料來源的配置
<!-- 連線池配置 -->
<bean id="dataSource" 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="dataSource" />
<!-- 載入jpa註解類 -->
<property name="packagesToScan">
<list>
<value>cn.lzh.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="cn.lzh.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的配置
<jaxrs:server id="customerService" address="/customerService">
<jaxrs:serviceBeans>
<bean class="cn.lzh.crm.service.impl.CustomerServiceImpl"></bean>
</jaxrs:serviceBeans>
</jaxrs:server>
- Customer服務的配置
Customer介面的配置
//查詢所有未關聯客戶列表
@Path("/noassociationcustomers")
@GET
@Produces({"application/xml","application/json"})
public List<Customer> findNoAssociationCustomers();
//查詢關聯到指定定區的客戶列表
@Path("/associationfixedreacustomers/{fixedareaId}")
@GET
@Produces({"application/xml","application/json"})
public List<Customer> findHashAssociationFixedAreaCustomers(@PathParam("fixedareaId")String fixedAreaId);
//將客戶關聯到定區上,將所有客戶id拼成字串1,2,3
@Path("/associationcustomerstofixedarea/")
@PUT//修改操作
public void associationCustomersTodixedArea(@QueryParam("customerIdStr")String customerIdStr,@QueryParam("fixedAreaId")String fixedAreaId);
Customer實現類的配置
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService{
//註冊Dao物件
@Autowired
private CustomerRepository customerRepository;
//呼叫dao層查詢未關聯定區id的客戶列表
@Override
public List<Customer> findNoAssociationCustomers() {
//fixAreaID is null
return customerRepository.findByFixedAreaIdIsNull();
}
//呼叫dao層查詢已關聯定區id的客戶列表
@Override
public List<Customer> findHashAssociationFixedAreaCustomers(String fixedAreaId) {
// TODO fixAreaID is
return customerRepository.findByFixedAreaId(fixedAreaId);
}
//呼叫dao層修改客戶表的定區編號
@Override
public void associationCustomersTodixedArea(String customerIdStr, String fixedAreaId) {
// TODO Auto-generated method stub
//切割字串1,2,3
String[] customerIdArray = customerIdStr.split(",");
for (String idStr : customerIdArray) {
Integer id = Integer.parseInt(idStr);
//update操作
customerRepository.updateFixedAreaId(fixedAreaId,id);
}
}
4.Dao介面的設定
//查詢未關聯定區的客戶,springdata 的規則
public List<Customer> findByFixedAreaIdIsNull();
//查詢已關聯定區的客戶
public List<Customer> findByFixedAreaId(String fixedAreaId);
//將客戶關聯定區,即修改客戶表的定區id
@Query("update Customer set fixedAreaId = ? where id = ?")
@Modifying
public void updateFixedAreaId(String fixedAreaId, Integer id);
Dao介面要實現JpaRepository<Customer, Integer>介面
Juite單元測試
@Autowired
private CustomerService customerService;
//測試客戶沒有關聯定區資料
@Test
public void testFindNoAssociationCustomers() {
System.out.println(customerService.findNoAssociationCustomers());
}
//測試客戶已關聯定區
@Test
public void testFindHashAssociationFixedAreaCustomers() {
System.out.println(customerService.findHashAssociationFixedAreaCustomers("dq001"));
}
//測試定區關聯使用者,即修改使用者的定區ID
@Test
public void testAssociationCustomersTodixedArea() {
customerService.associationCustomersTodixedArea("1,2", "dq001");
}
釋出WebService服務
容易出錯的地方
原因
沒有加@XmlRootElement 註解