天天看點

WebService接口服務一個簡單的接口服務

接口WebService

  • 一個簡單的接口服務
    • 開發環境
    • 開發工具
    • 技術實作
    • 主要配置檔案
      • jdbc.properties
      • srping-cxf.xml
      • spring-mybatis.xml
    • 項目結構
    • 主要代碼
      • 接口
      • 接口實作類
    • 問題與總結
      • 問題1 No operation was found
      • 問題2 Date類型資料寫入和讀取都隻有年月日
        • 原因:
        • 解決辦法
        • mapper.xml
    • GitHub位址

一個簡單的接口服務

最近寫了一個接口服務,發現有許多東西自己都忘了,記在筆記本上又怕忘了,是以想想還是寫在微網誌上吧。

開發環境

tomcat 8.0.52、maven 3.3.9、JDK1.8

開發工具

Intellij IDEA

技術實作

Spring + SpringMVC + Mybatis

主要配置檔案

jdbc.properties

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=xiongzx123
jdbc.password=xiongzx123
#定義初始連接配接數
jdbc.initialSize=0
#定義最大連接配接數
jdbc.maxActive=20
#定義最大空閑
jdbc.maxIdle=20
#定義最小空閑
jdbc.minIdle=1
#定義最長等待時間
jdbc.maxWait=60000
           

srping-cxf.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:jaxws="http://cxf.apache.org/jaxws"
       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">

    <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-xml.xml"/>-->

    <bean id="ServiceImpl" class="com.sunland.webservice.impl.WebServiceImpl"/>
    <jaxws:endpoint id="financeServiceWsdl" implementor="#ServiceImpl" address="/vioService">
        <jaxws:inInterceptors>
            <bean class="com.sunland.webservice.WebServiceInterceptor"/>
        </jaxws:inInterceptors>
    </jaxws:endpoint>
</beans>
           

spring-mybatis.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 自動掃描 -->
    <context:component-scan base-package="com.sunland"/>

    <context:property-placeholder location="classpath*:jdbc.properties"/>


    <!--+++++++++++++++業務庫++++++++++++++++++-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!-- spring和MyBatis整合,不需要mybatis的配置映射檔案 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- mybatis配置檔案的位置 -->
        <!--<property name="configLocation" value="classpath:mybatis-config.xml"/>-->
        <!-- 自動掃描mapping.xml檔案 -->
        <property name="mapperLocations" value="classpath:/com/sunland/mapping/*.xml"/>
        <property name="typeAliasesPackage" value="com.sunland.pojo"/>
    </bean>
    <!-- DAO接口所在包名,Spring會自動查找其下的類 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.sunland.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>
    <!--oracle seq-->
    <bean id="incre" class="org.springframework.jdbc.support.incrementer.OracleSequenceMaxValueIncrementer">
        <property name="incrementerName" value="vioid_seq"/><!-- ①指定序列名-->
        <property name="dataSource" ref="dataSource"/> <!-- ②設定資料源-->
    </bean>
</beans>
           

項目結構

WebService接口服務一個簡單的接口服務

主要代碼

接口

package com.sunland.webservice;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface IWebService {
    /**
     * 推送案件資料
     *
     * @param requestXml
     * @return
     * @throws Exception
     */
    @WebMethod(operationName = "Push_VioInfo")
    @WebResult(name = "result")
    String PushVioInfo(@WebParam(name = "requestXml") String requestXml) throws Exception;

    /**
     * 查詢車輛資訊
     *
     * @param requestXml
     * @return
     * @throws Exception
     */
    @WebMethod(operationName = "Query_VehicleInfo")
    @WebResult(name = "result")
    String QueryVehicleInfo(@WebParam(name = "requestXml") String requestXml) throws Exception;
}

           

接口實作類

package com.sunland.webservice.impl;

import com.sunland.pojo.VehicleinfoRequest;
import com.sunland.pojo.Vio;
import com.sunland.service.IVehicleService;
import com.sunland.service.IVioService;
import com.sunland.utils.XMLUtils;
import com.sunland.webservice.IWebService;

import javax.annotation.Resource;
import javax.jws.WebService;

@WebService(
        endpointInterface = "com.sunland.webservice.IWebService",
        targetNamespace = "http://webservice.sunland.com/"
)
public class WebServiceImpl implements IWebService {
    @Resource
    IVioService vioService;
    @Resource
    IVehicleService vehicleService;

    @Override
    public String PushVioInfo(String requestXml) throws Exception {
        Vio vio = XMLUtils.parseDTVioInfoXml(requestXml);
        String result = vioService.insertVio(vio);
        return result;
    }

    @Override
    public String QueryVehicleInfo(String requestXml) throws Exception {
        //解析xml封包
        VehicleinfoRequest vr = XMLUtils.parseQueryVehicleRequestXml(requestXml);
        System.out.println("車輛資訊:" + vr);
        String result = vehicleService.QueryVehicleInfo(vr);
        return result;
    }
}
           

問題與總結

問題1 No operation was found

WebService接口服務一個簡單的接口服務

原因因為我的接口和實作類不在同一個包下面,是以會出現這類情況。

解決辦法:

在接口實作類上加上

@WebService(

endpointInterface = “com.sunland.webservice.IWebService”,

targetNamespace = “http://webservice.sunland.com/”

)

如下圖所示:

WebService接口服務一個簡單的接口服務

問題2 Date類型資料寫入和讀取都隻有年月日

原因:

因為我的mapper是用插件生成的,是以,jdbcType=Date;此處的Date是java.sql.Date,網上查過了,說是該類型隻有年月日。

解決辦法

讀取資料庫Date類型資料需要時分秒,需要将mapper.xml檔案内resultMap的jdbcType改為TIMESTAMP,寫入資料庫的資料需要時分秒的,需要在mapper.xml相應的sql内将jdbcType改為TIMESTAMP

WebService接口服務一個簡單的接口服務
WebService接口服務一個簡單的接口服務

mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sunland.dao.IVioImgDao">
    <resultMap id="BaseResultMap" type="com.sunland.pojo.VioImg">
        <id column="PID" jdbcType="DECIMAL" property="pid"/>
        <result column="VIOID" jdbcType="DECIMAL" property="vioid"/>
        <result column="INSERTTIME" jdbcType="TIMESTAMP" property="inserttime"/>
        <result column="IMGINDEX" jdbcType="DECIMAL" property="imgindex"/>
        <result column="JLLX" jdbcType="CHAR" property="jllx"/>
        <result column="UPDATETIME" jdbcType="TIMESTAMP" property="updatetime"/>
    </resultMap>
    <resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="com.sunland.pojo.VioImg">
        <result column="CONTENT" jdbcType="BLOB" property="content"/>
    </resultMap>
    <sql id="Base_Column_List">
    PID, VIOID, INSERTTIME, IMGINDEX, JLLX, UPDATETIME
  </sql>
    <sql id="Blob_Column_List">
    CONTENT
  </sql>
    <insert id="insertSelective" parameterType="com.sunland.pojo.VioImg">
        insert into VIO_IMG
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <!--<if test="pid != null">-->
            <!--PID,-->
            <!--</if>-->
            PID,
            <if test="vioid != null">
                VIOID,
            </if>
            <if test="inserttime != null">
                INSERTTIME,
            </if>
            <if test="imgindex != null">
                IMGINDEX,
            </if>
            <if test="jllx != null">
                JLLX,
            </if>
            <if test="updatetime != null">
                UPDATETIME,
            </if>
            <if test="content != null">
                CONTENT,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <!--<if test="pid != null">-->
            <!--#{pid,jdbcType=DECIMAL},-->
            <!--</if>-->
            vioimg_pid_seq.nextval,
            <if test="vioid != null">
                #{vioid,jdbcType=DECIMAL},
            </if>
            <if test="inserttime != null">
                #{inserttime,jdbcType=TIMESTAMP},
            </if>
            <if test="imgindex != null">
                #{imgindex,jdbcType=DECIMAL},
            </if>
            <if test="jllx != null">
                #{jllx,jdbcType=CHAR},
            </if>
            <if test="updatetime != null">
                #{updatetime,jdbcType=TIMESTAMP},
            </if>
            <if test="content != null">
                #{content,jdbcType=BLOB},
            </if>
        </trim>
    </insert>
</mapper>
           

GitHub位址

項目已上傳至GitHub,裡面包括圖檔加密解密轉碼的内容,有興趣的可以看看。

GitHub項目位址