天天看点

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项目地址