Webservice

                                 Webservice


 

1 soap

在http基础上封装的统一的webservice协议(传输协议和安全协议)

有统一框架和协议,cxf

2 http

在rest风格基础上,企业发布公共开源的接口用

直接使用http访问,不需要统一的框架


    CXF


1、CXF框架概念介绍

  Apache CXF 是一个开源的 WebService 框架,CXF可以用来构建和开发 WebService,这些服务可以支持多种协议,比如:SOAP、POST/HTTP、HTTP ,CXF 大大简化了WebService并且可以天然地和 Spring 进行无缝集成。CXF是 Celtrix (ESB框架)和 XFire(webserivice) 合并而成,核心是org.apache.cxf.Bus(总线),类似于Spring的 ApplicationContext,CXF默认是依赖于Spring的,另 CXF 发行包中的jar,如果全部放到lib中,需要 JDK1.6 及以上,否则会报JAX-WS版本不一致的问题。CXF 内置了Jetty服务器 ,它是servlet容器。

2、CXF框架特点

  A、与Spring、Servlet做了无缝对接,cxf框架里面集成了Servlet容器Jetty

       B、支持注解的方式来发布webservice

       C、能够显示一个webservice的服务列表

       D、能够添加拦截器:输入拦截器、输出拦截器 :输入日志信息拦截器、输出日志拦截器、用户权限认证的拦截器


3.使用CXF框架+Spring框架+Mybatis框架集成发布一个WebService



项目目录



pom.xml的依赖

<dependencies>
   
    <!-- 1.数据库连接 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.37</version>
		</dependency>
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>
		<!-- 2.mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.2.8</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.2.2</version>
		</dependency>
		<!-- 3.Spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.0.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.0.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.0.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>4.0.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.0.0.RELEASE</version>
		</dependency>

		<!-- 4.辅助 -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
			<version>3.1</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.7</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.7</version>
		</dependency>
		
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.2.4</version>
		</dependency>
		
		<!-- 5.CXF -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>3.0.5</version>
		</dependency>
		
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
			<version>3.0.5</version>
		</dependency>
		
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-ws-security</artifactId>
			<version>3.0.5</version>
		</dependency>
		
		<dependency>
			<groupId>org.apache.ws.security</groupId>
			<artifactId>wss4j</artifactId>
			<version>1.5.2</version>
		</dependency>
		
		<!-- 6.rest -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxrs</artifactId>
			<version>3.0.5</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http-jetty</artifactId>
			<version>3.0.5</version>
		</dependency>
  </dependencies>


jaxrs.xsd

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements. See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership. The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License. You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied. See the License for the
  specific language governing permissions and limitations
  under the License.
-->
<xsd:schema xmlns="http://cxf.apache.org/jaxrs" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:cxf-beans="http://cxf.apache.org/configuration/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" targetNamespace="http://cxf.apache.org/jaxrs" xmlns:tns="http://cxf.apache.org/jaxrs" elementFormDefault="qualified" attributeFormDefault="unqualified">

  <xsd:import namespace="http://www.springframework.org/schema/beans" schemaLocation="http://www.springframework.org/schema/beans/spring-beans.xsd"/>
  <xsd:import namespace="http://cxf.apache.org/configuration/beans" schemaLocation="http://cxf.apache.org/schemas/configuration/cxf-beans.xsd"/>
  <xsd:include schemaLocation="http://cxf.apache.org/schemas/jaxrs-common.xsd"/>
  
  <xsd:element name="server">
    <xsd:complexType>
      <xsd:complexContent>
        <xsd:extension base="beans:identifiedType">
          <xsd:all>
            <xsd:element name="executor" type="xsd:anyType" minOccurs="0"/>
            <xsd:element name="features" type="xsd:anyType" minOccurs="0"/>
            <xsd:element name="binding" type="xsd:anyType" minOccurs="0"/>  
            <xsd:element name="dataBinding" type="xsd:anyType" minOccurs="0"/>
            <xsd:element name="inInterceptors" type="xsd:anyType" minOccurs="0"/>
            <xsd:element name="inFaultInterceptors" type="xsd:anyType" minOccurs="0"/>
            <xsd:element name="invoker" type="xsd:anyType" minOccurs="0"/>
            <xsd:element name="serviceFactories" type="xsd:anyType" minOccurs="0"/>
            <xsd:element name="outInterceptors" type="xsd:anyType" minOccurs="0"/>
            <xsd:element name="outFaultInterceptors" type="xsd:anyType" minOccurs="0"/>
            <xsd:element name="properties" type="beans:mapType" minOccurs="0"/>
            <xsd:element name="serviceBeans" type="xsd:anyType" minOccurs="0"/>            
            <xsd:element name="modelBeans" type="xsd:anyType" minOccurs="0"/>
            <xsd:element name="model" type="model" minOccurs="0"/>
            <xsd:element name="providers" type="xsd:anyType" minOccurs="0"/>
            <xsd:element name="extensionMappings" type="xsd:anyType" minOccurs="0"/>
            <xsd:element name="languageMappings" type="xsd:anyType" minOccurs="0"/>
            <xsd:element name="schemaLocations" type="schemasType" minOccurs="0"/>
            <xsd:element name="resourceComparator" type="xsd:anyType" minOccurs="0"/> 
          </xsd:all>
          <xsd:attributeGroup ref="cxf-beans:beanAttributes"/>
          <xsd:attribute name="address" type="xsd:string"/>
          <xsd:attribute name="bus" type="xsd:string"/>
          <xsd:attribute name="serviceClass" type="xsd:string"/>                    
          <xsd:attribute name="transportId" type="xsd:string"/>
          <xsd:attribute name="modelRef" type="xsd:string"/>
          <xsd:attribute name="bindingId" type="xsd:string"/>          
          <xsd:attribute name="staticSubresourceResolution" type="xsd:boolean"/>          
          <xsd:attribute name="beanNames" type="xsd:string"/>
          <xsd:attribute name="serviceName" type="xsd:QName"/>
          <xsd:attribute name="docLocation" type="xsd:string"/>
          <xsd:attribute name="publishedEndpointUrl" type="xsd:string"/>
          <xsd:attribute name="basePackages" type="xsd:string"/>
          <xsd:attribute name="serviceAnnotation" type="xsd:string"/>
          <xsd:attribute name="publish" type="xsd:boolean"/>
        </xsd:extension>
      </xsd:complexContent>
    </xsd:complexType>
  </xsd:element>
  <xsd:element name="client">
    <xsd:complexType>
      <xsd:complexContent>
        <xsd:extension base="beans:identifiedType">
          <xsd:all>
            <xsd:element name="executor" type="xsd:anyType" minOccurs="0"/>
            <xsd:element name="features" type="xsd:anyType" minOccurs="0"/>
            <xsd:element name="binding" type="xsd:anyType" minOccurs="0"/>
            <xsd:element name="dataBinding" type="xsd:anyType" minOccurs="0"/>
            <xsd:element name="inInterceptors" type="xsd:anyType" minOccurs="0"/>
            <xsd:element name="inFaultInterceptors" type="xsd:anyType" minOccurs="0"/>
            <xsd:element name="outInterceptors" type="xsd:anyType" minOccurs="0"/>
            <xsd:element name="outFaultInterceptors" type="xsd:anyType" minOccurs="0"/>
            <xsd:element name="properties" type="beans:mapType" minOccurs="0"/>
            <xsd:element name="providers" type="xsd:anyType" minOccurs="0"/>
            <xsd:element name="modelBeans" type="xsd:anyType" minOccurs="0"/>
            <xsd:element name="model" type="model" minOccurs="0"/>
            <xsd:element name="headers" type="xsd:anyType" minOccurs="0"/> 
            <xsd:element name="schemaLocations" type="schemasType" minOccurs="0"/>
          </xsd:all>
          <xsd:attributeGroup ref="cxf-beans:beanAttributes"/>
          <xsd:attribute name="address" type="xsd:string"/>
          <xsd:attribute name="serviceClass" type="xsd:string"/>
          <xsd:attribute name="inheritHeaders" type="xsd:boolean"/>
          <xsd:attribute name="bus" type="xsd:string"/>
          <xsd:attribute name="transportId" type="xsd:string"/>
          <xsd:attribute name="bindingId" type="xsd:string"/>
          <xsd:attribute name="modelRef" type="xsd:string"/>
          <xsd:attribute name="username" type="xsd:string"/>
          <xsd:attribute name="password" type="xsd:string"/>
          <xsd:attribute name="serviceName" type="xsd:QName"/>
          <xsd:attribute name="threadSafe" type="xsd:boolean"/>
        </xsd:extension>
      </xsd:complexContent>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

 jaxws.xsd

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements. See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership. The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License. You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied. See the License for the
  specific language governing permissions and limitations
  under the License.
-->
<xsd:schema xmlns="http://cxf.apache.org/jaxws"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:cxf-beans="http://cxf.apache.org/configuration/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  targetNamespace="http://cxf.apache.org/jaxws" 
  elementFormDefault="qualified"
  attributeFormDefault="unqualified"  >

  <xsd:import namespace="http://www.springframework.org/schema/beans" schemaLocation="http://www.springframework.org/schema/beans/spring-beans.xsd"/>
  <xsd:import namespace="http://cxf.apache.org/configuration/beans" schemaLocation="http://cxf.apache.org/schemas/configuration/cxf-beans.xsd"/>

  <xsd:element name="endpoint">
    <xsd:annotation>
      <xsd:documentation>Configures a JAX-WS server. This element is associated with the org.apache.cxf.jaxws.EndpointImpl object.</xsd:documentation>
    </xsd:annotation>
    <xsd:complexType>
      <xsd:complexContent>
        <xsd:extension base="beans:identifiedType">
          <xsd:all>
            <xsd:element name="binding" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Configures the message binding used by the endpoint. Message bindings are configured using implementations of the 
                  org.apache.cxf.binding.BindingFactory interface. The SOAP binding is configured using the soap:soapBinding bean.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="dataBinding" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Configures the data binding used by the endpoint. The class implementing the JAXB data binding is org.apache.cxf.jaxb.JAXBDataBinding.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="executor" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Configures a Java executor to handle the service.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="features" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Specifies a list of beans that configure advanced features like WS-RM.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="handlers" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Specifies a list of JAX-WS handlers to add to the endpoint's processing chain.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="implementor" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Configures the bean implementing the service. If this child is used you should not use the implementor attribute.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="inInterceptors" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Specifies a list of interceptors to process incoming requests.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="inFaultInterceptors" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Specifies a list of interceptors to process incoming fault messages.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="invoker" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Specifies an implementation of the org.apache.cxf.service.Invoker interface to be used by the service. The Invoker 
                  implementation controls how a service is invoked. For example, it controls if each request is handled by a new instance of the 
                  service implementation or if state is preserved across invocations.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="outInterceptors" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Specifies a list of interceptors to process outgoing responses.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="outFaultInterceptors" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Specifies a list of interceptors to process outgoing fault messages.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="properties" type="beans:mapType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Specifies a map of properties that are passed to the endpoint.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="schemaLocations" type="schemasType" minOccurs="0"/>
            <xsd:element name="serviceFactory" type="xsd:anyType" minOccurs="0"/>
          </xsd:all>
          <xsd:attributeGroup ref="cxf-beans:beanAttributes"/>
          <xsd:attribute name="address" type="xsd:string">
            <xsd:annotation>
              <xsd:documentation>Specifies the HTTP address of the endpoint. This value will override the value specified in the services 
                contract.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
          <xsd:attribute name="bindingUri" type="xsd:string">
            <xsd:annotation>
              <xsd:documentation>Specifies the ID of the data binding the service will use. For SOAP bindings the IDs are defined in the JAX-WS 
                specification. For other data bindings, the ID is the namespace of the WSDL extensions used to configure the
                binding.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
          <xsd:attribute name="bus" type="xsd:string">
            <xsd:annotation>
              <xsd:documentation>Specifies the ID of the Spring bean configuring the bus managing the endpoint.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
          <xsd:attribute name="implementor" type="xsd:string">
            <xsd:annotation>
              <xsd:documentation>Specifies the class implementing the service. You can specify the implementation class using either the class 
                name or an ID reference to a Spring bean configuring the implementation class. This class needs to be on the 
                classpath.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
          <xsd:attribute name="implementorClass" type="xsd:string">
            <xsd:annotation>
              <xsd:documentation>Specifies the name of the class implementing the service. This attribute is useful when you specify the implementor 
                with the ref bean which is wrapped by using Spring AOP.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
          <xsd:attribute name="publish" type="xsd:boolean" default="true">
            <xsd:annotation>
              <xsd:documentation>Specifies if the service should be automatically published.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
          <xsd:attribute name="publishedEndpointUrl" type="xsd:string"/>
          <xsd:attribute name="endpointName" type="xsd:QName">
            <xsd:annotation>
              <xsd:documentation>Specifies the value of the service's WSDL port element's name attribute.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
          <xsd:attribute name="serviceName" type="xsd:QName">
            <xsd:annotation>
              <xsd:documentation>Specifies the value of the service's WSDL service element's name attribute.</xsd:documentation>
            </xsd:annotation>            
          </xsd:attribute>
          <xsd:attribute name="transportId" type="xsd:string">
        	<xsd:annotation>
              <xsd:documentation>Specifies the transportId that endpoint will use, it will override the transport which is defined in the wsdl.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>  
          <xsd:attribute name="wsdlLocation" type="xsd:string">
            <xsd:annotation>
              <xsd:documentation>Specifies the location of the endpoint's WSDL contract. The WSDL contract's location is relative to the folder 
                from which the service is deployed.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
        </xsd:extension>
      </xsd:complexContent>
    </xsd:complexType>
  </xsd:element>
  
  <xsd:element name="server">
    <xsd:annotation>
      <xsd:documentation>Configures a JAX-WS server. The element is associated with the org.apache.cxf.jaxws.support.JaxWsServerFactoryBean object.</xsd:documentation>
    </xsd:annotation>
    <xsd:complexType>
      <xsd:complexContent>
        <xsd:extension base="beans:identifiedType">
          <xsd:all>
            <xsd:element name="binding" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Configures the message binding used by the endpoint. Message bindings are configured using implementations of the 
                  org.apache.cxf.binding.BindingFactory interface. The SOAP binding is configured using the soap:soapBinding bean.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="dataBinding" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Configures the data binding used by the endpoint. The class implementing the JAXB data binding is
                  org.apache.cxf.jaxb.JAXBDataBinding.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="executor" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Configures a Java executor to handle the service.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="features" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Specifies a list of beans that configure advanced features like WS-RM.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="handlers" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Specifies a list of JAX-WS handlers to add to the endpoint's processing chain.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="inInterceptors" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Specifies a list of interceptors to process incoming requests.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="inFaultInterceptors" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Specifies a list of interceptors to process incoming fault messages.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="invoker" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Specifies an implementation of the org.apache.cxf.service.Invoker interface to be used by the service. The Invoker 
                  implementation controls how a service is invoked. For example, it controls if each request is handled by a new instance of the 
                  service implementation or if state is preserved across invocations.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="outInterceptors" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Specifies a list of interceptors to process outgoing responses.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="outFaultInterceptors" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Specifies a list of interceptors to process outgoing fault messages.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="properties" type="beans:mapType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Specifies a map of properties that are passed to the endpoint.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="schemaLocations" type="schemasType" minOccurs="0"/>
            <xsd:element name="serviceBean" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Configures the bean implementing the service. If this child is used you should not use the serviceBean attribute.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="serviceFactory" type="xsd:anyType" minOccurs="0"/>
          </xsd:all>
          <xsd:attributeGroup ref="cxf-beans:beanAttributes"/>
          <xsd:attribute name="address" type="xsd:string">
            <xsd:annotation>
              <xsd:documentation>Specifies the HTTP address of the endpoint. This value will override the value specified in the services 
                contract.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
          <xsd:attribute name="bindingId" type="xsd:string">
            <xsd:annotation>
              <xsd:documentation>Specifies the ID of the data binding the service will use. For SOAP bindings the IDs are defined in the JAX-WS 
                specification. For other data bindings, the ID is the namespace of the WSDL extensions used to configure the
                binding.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
          <xsd:attribute name="bus" type="xsd:string">
            <xsd:annotation>
              <xsd:documentation>Specifies the ID of the Spring bean configuring the bus managing the endpoint.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
          <xsd:attribute name="serviceClass" type="xsd:string">
            <xsd:annotation>
              <xsd:documentation>Specifies the name of the class implementing the service. This attribute is useful when you specify the implementor 
                with the ref bean which is wrapped by using Spring AOP.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
          <xsd:attribute name="serviceBean" type="xsd:string">
            <xsd:annotation>
              <xsd:documentation>Specifies the class implementing the service. You can specify the implementation class using either the class 
                name or an ID reference to a Spring bean configuring the implementation class. This class needs to be on the 
                classpath.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
          <xsd:attribute name="start" type="xsd:boolean" default="true">
            <xsd:annotation>
              <xsd:documentation>Specifies if the service should be automatically published.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
           <xsd:attribute name="transportId" type="xsd:string">
        	<xsd:annotation>
              <xsd:documentation>Specifies the transportId that endpoint will use, it will override the transport which is defined in the wsdl.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
          <xsd:attribute name="wsdlLocation" type="xsd:string">
            <xsd:annotation>
              <xsd:documentation>Specifies the location of the endpoint's WSDL contract. The WSDL contract's location is relative to the folder 
                from which the service is deployed.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
          <xsd:attribute name="endpointName" type="xsd:QName">
            <xsd:annotation>
              <xsd:documentation>Specifies the value of the service's WSDL port element's name attribute.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
          <xsd:attribute name="serviceName" type="xsd:QName">
            <xsd:annotation>
              <xsd:documentation>Specifies the value of the service's WSDL service element's name attribute.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
        </xsd:extension>
      </xsd:complexContent>
    </xsd:complexType>
  </xsd:element>

  <xsd:element name="client">
    <xsd:annotation>
      <xsd:documentation>Configures a JAX-WS client.</xsd:documentation>
    </xsd:annotation>
    <xsd:complexType>
      <xsd:complexContent>
        <xsd:extension base="beans:identifiedType">
          <xsd:all>
            <xsd:element name="binding" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Configures the message binding used by the endpoint. Message bindings are configured using implementations of the 
                  org.apache.cxf.binding.BindingFactory interface. The SOAP binding is configured using the soap:soapBinding bean.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="dataBinding" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Configures the data binding used by the endpoint. The class implementing the JAXB data binding is org.apache.cxf.jaxb.JAXBDataBinding.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="features" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Specifies a list of beans that configure advanced features like WS-RM.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="handlers" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Specifies a list of JAX-WS handlers to add to the endpoint's processing chain.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="inInterceptors" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Specifies a list of interceptors to process incoming responses.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="inFaultInterceptors" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Specifies a list of interceptors to process incoming fault messages.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="outInterceptors" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Specifies a list of interceptors to process outgoing requests.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="outFaultInterceptors" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Specifies a list of interceptors to process outgoing fault messages.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="properties" type="beans:mapType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Specifies a map of properties that are passed to the endpoint.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
            <xsd:element name="conduitSelector" type="xsd:anyType" minOccurs="0">
              <xsd:annotation>
                <xsd:documentation>Specifies a org.apache.cxf.endpoint.ConduitSelector implementation. ConduitSelector implementations override the 
                  strategy used to select the Conduit used to process outgoing messages.</xsd:documentation>
              </xsd:annotation>
            </xsd:element>
          </xsd:all>
          <xsd:attributeGroup ref="cxf-beans:beanAttributes"/>
          <xsd:attribute name="address" type="xsd:string">
            <xsd:annotation>
              <xsd:documentation>Specifies the HTTP address of the endpoint on which the client makes requests. This value will override the value 
                specified in the services contract.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
          <xsd:attribute name="bindingId" type="xsd:string">
            <xsd:annotation>
              <xsd:documentation>Specifies the ID of the data binding the client will use. For SOAP bindings the IDs are defined in the JAX-WS 
                specification. For other data bindings, the ID is the namespace of the WSDL extensions used to configure the
                binding.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
          <xsd:attribute name="bus" type="xsd:string">
            <xsd:annotation>
              <xsd:documentation>Specifies the ID of the Spring bean configuring the bus managing the endpoint.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
          <xsd:attribute name="endpointName" type="xsd:QName">
            <xsd:annotation>
              <xsd:documentation>Specifies the value of the WSDL port element's name attribute for the service on which the client is making 
                requests.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
          <xsd:attribute name="password" type="xsd:string">
            <xsd:annotation>
              <xsd:documentation>Specifies a password that is used for simple username/password authentication.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
          <xsd:attribute name="serviceClass" type="xsd:string">
            <xsd:annotation>
              <xsd:documentation>Specifies the name of the class implementing the client.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
          <xsd:attribute name="serviceName" type="xsd:QName">
            <xsd:annotation>
              <xsd:documentation>Specifies the value of the WSDL service element's name attribute for the service on which the client is making 
                requests.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
          <xsd:attribute name="username" type="xsd:string">
            <xsd:annotation>
              <xsd:documentation>Specifies a password that is used for simple username/password authentication.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
          <xsd:attribute name="transportId" type="xsd:string">
        	<xsd:annotation>
              <xsd:documentation>Specifies the transportId that endpoint will use, it will override the transport which is defined in the wsdl.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
          <xsd:attribute name="wsdlLocation" type="xsd:string">
            <xsd:annotation>
              <xsd:documentation>Specifies the location of the endpoint's WSDL contract. The WSDL contract's location is relative to the folder 
                from which the client is deployed.</xsd:documentation>
            </xsd:annotation>
          </xsd:attribute>
        </xsd:extension>
      </xsd:complexContent>
    </xsd:complexType>
  </xsd:element>
  
  <xsd:complexType name="schemasType">
    <xsd:sequence>
      <xsd:element name="schemaLocation" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>
  
</xsd:schema>

jaxrs.xsd 和 jaxws.xsd 的添加
把它们的内容保存成两个文档
选择User Specified Entries(我已经添加好了),点击add, 选择File System,选择保存的目录文件,点击完成





beans标签的jaxws和jaxrs的添加如下

xmlns:jaxws="http://cxf.apache.org/jaxws"

在xsi:schemaLocation中添加
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
jaxrs的添加也类似

applicationContext.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"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	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://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/jdbc 
       http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
               http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
                http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://cxf.apache.org/jaxws
        http://cxf.apache.org/schemas/jaxws.xsd
        http://cxf.apache.org/jaxrs
        http://cxf.apache.org/schemas/jaxrs.xsd
       ">
<context:property-placeholder location="classpath:dbConfig.properties" />
 <context:component-scan base-package="com.atguigu.service" />
	
	
    
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${driverClassName}" />
		<property name="url" value="${url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:mybatis-config.xml" />
		<!-- 扫描java bean,自动使用别名 -->
    	<property name="typeAliasesPackage" value="com.atguigu.bean"/>
    	<property name="mapperLocations" value="classpath:mapper/*.xml"/>
    	
	</bean>

	<bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.atguigu.mapper" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean" />
	</bean>
    <!-- 用websevice方式暴露接口-->
	<jaxws:endpoint address="/ws" implementorClass="com.atguigu.server.TestServerInf">
		<jaxws:implementor>
			<bean class="com.atguigu.server.TestServerImp"></bean>
		</jaxws:implementor>
	</jaxws:endpoint>
	<jaxws:endpoint address="/login" implementorClass="com.atguigu.server.LoginServerInf">
		<jaxws:implementor>
			<bean class="com.atguigu.server.LoginServerImp"></bean>
		</jaxws:implementor>
	</jaxws:endpoint>
	 <!-- 用restful方式暴露接口-->
	<jaxrs:server address="/loginRest">
		<jaxrs:serviceBeans>
			<bean class="com.atguigu.server.LoginServerImp"></bean>
		</jaxrs:serviceBeans>
	</jaxrs:server>
</beans>

 


dbConfig.properties

#oracle.jdbc.driver.OracleDriver
#jdbc:oracle:thin:@localhost:1521:v6orcl
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/atguigu_user?useUnicode=true&characterEncoding=UTF8
jdbc.username=root
jdbc.password=123456

log4j.properties

log4j.rootLogger=DEBUG, b
log4j.appender.b=org.apache.log4j.ConsoleAppender
log4j.appender.b.layout=org.apache.log4j.PatternLayout
log4j.appender.b.layout.ConversionPattern=%5p  %m%n
#
log4j.logger.org.mybatis=DEBUG
#log4j.logger.org.apache.struts2=on
#log4j.logger.com.opensymphony.xwork2=off
log4j.logger.com.ibatis=on
log4j.logger.org.apache.cxf=off
#log4j.logger.org.hibernate=OFF 
log4j.logger.org.springframework=off
#log4j.logger.com.opensymphony.xwork2=ERROR 

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<mappers>
	  
	</mappers>
</configuration>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>mall_user</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <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> 
  
 
   <!-- 不用SpringMVC的DispatcherServlet,而用cxf框架的CXFServlet-->
  <servlet>
  	<servlet-name>cxf</servlet-name>
  	<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
 	
 	 
  </servlet>
  <servlet-mapping>
      <servlet-name>cxf</servlet-name>
      <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

bean的类
T_MALL_USER_ACCOUNT.java
用restful方式时需要添加相应的注解进行bean类的映射,webservice时则不用

package com.atguigu.bean;

import javax.ws.rs.FormParam;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class T_MALL_USER_ACCOUNT {
	
	@FormParam("id")
	private int id;
	@FormParam("yh_mch")
	private String yh_mch;
	@FormParam("yh_nch")
	private String yh_nch;
	@FormParam("yh_mm")
	private String yh_mm;
	@FormParam("yh_xm")
	private String yh_xm;
	@FormParam("yh_shjh")
	private String yh_shjh;
	@FormParam("yh_yx")
	private String yh_yx;
	@FormParam("yh_tx")
	private String yh_tx;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getYh_mch() {
		return yh_mch;
	}

	public void setYh_mch(String yh_mch) {
		this.yh_mch = yh_mch;
	}

	public String getYh_nch() {
		return yh_nch;
	}

	public void setYh_nch(String yh_nch) {
		this.yh_nch = yh_nch;
	}

	public String getYh_mm() {
		return yh_mm;
	}

	public void setYh_mm(String yh_mm) {
		this.yh_mm = yh_mm;
	}

	public String getYh_xm() {
		return yh_xm;
	}

	public void setYh_xm(String yh_xm) {
		this.yh_xm = yh_xm;
	}

	public String getYh_shjh() {
		return yh_shjh;
	}

	public void setYh_shjh(String yh_shjh) {
		this.yh_shjh = yh_shjh;
	}

	public String getYh_yx() {
		return yh_yx;
	}

	public void setYh_yx(String yh_yx) {
		this.yh_yx = yh_yx;
	}

	public String getYh_tx() {
		return yh_tx;
	}

	public void setYh_tx(String yh_tx) {
		this.yh_tx = yh_tx;
	}

}

Mapper包下的LoginMapper.java 

package com.atguigu.mapper;

import com.atguigu.bean.T_MALL_USER_ACCOUNT;



public interface LoginMapper {

	T_MALL_USER_ACCOUNT select_user(T_MALL_USER_ACCOUNT user );

}

Server包下的(类似于SSM的Controller包)

  LoginServerImp.java

package com.atguigu.server;

import javax.ws.rs.BeanParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.springframework.beans.factory.annotation.Autowired;

import com.atguigu.bean.T_MALL_USER_ACCOUNT;
import com.atguigu.mapper.LoginMapper;
import com.atguigu.service.LoginServiceInf;
import com.google.gson.Gson;
import com.mysql.fabric.xmlrpc.base.Param;

public class LoginServerImp implements LoginServerInf {

	@Autowired
	LoginServiceInf loginServiceInf;

	@Override
	@Path("login")
	@GET
	@Consumes("application/x-www-form-urlencoded")
	@Produces("application/json")
	public  String login(@BeanParam T_MALL_USER_ACCOUNT user) {
		T_MALL_USER_ACCOUNT select_user =  loginServiceInf.login(user);
		Gson gson = new Gson();
		return gson.toJson(select_user);
	}
}

LoginServerInf.java,该接口类需添加注解 @WebService

package com.atguigu.server;

import javax.jws.WebService;

import com.atguigu.bean.T_MALL_USER_ACCOUNT;
@WebService
public interface LoginServerInf {
	
	public  String login(T_MALL_USER_ACCOUNT user);
}

TestServerImp.java

package com.atguigu.server;

public class TestServerImp implements TestServerInf {

	@Override
	public String ping(String hello) {
		System.out.println("cxf接口调用:" + hello);
		return "pong";
	}

}

TestServerInf.java

package com.atguigu.server;

import javax.jws.WebService;

@WebService
public interface TestServerInf {

	public String ping(String hello);
}

service包下的类

LoginServiceImp.java

package com.atguigu.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.atguigu.bean.T_MALL_USER_ACCOUNT;
import com.atguigu.mapper.LoginMapper;

@Service
public class LoginServiceImp implements LoginServiceInf {

	@Autowired
	LoginMapper loginMapper;

	@Override
	public  T_MALL_USER_ACCOUNT login(T_MALL_USER_ACCOUNT user) {
		// TODO Auto-generated method stub
		return loginMapper.select_user(user);
	}

}

 LoginServiceInf.java

package com.atguigu.service;

import com.atguigu.bean.T_MALL_USER_ACCOUNT;

public interface LoginServiceInf {

	public T_MALL_USER_ACCOUNT login(T_MALL_USER_ACCOUNT user);
}

resources下的mapper目录的LoginMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper SYSTEM "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.atguigu.mapper.LoginMapper">

<select id="select_user" parameterType="com.atguigu.bean.T_MALL_USER_ACCOUNT" resultType="com.atguigu.bean.T_MALL_USER_ACCOUNT">
	select * from t_mall_user_account where yh_mch = #{yh_mch} and yh_mm = #{yh_mm}

</select>


</mapper>


数据库的创建与导入
创建utf-8的数据库atguigu_user
创建T_MALL_USER_ACCOUNT表

sql语句

CREATE TABLE `t_mall_user_account` (
  `Id` int(11) NOT NULL COMMENT '编号',
  `yh_mch` varchar(100) DEFAULT NULL COMMENT '用户名称',
  `yh_nch` varchar(100) DEFAULT NULL COMMENT '用户昵称',
  `yh_mm` varchar(100) DEFAULT NULL COMMENT '用户密码',
  `yh_xm` varchar(100) DEFAULT NULL COMMENT '用户姓名',
  `yh_shjh` varchar(100) DEFAULT NULL COMMENT '手机号',
  `yh_yx` varchar(100) DEFAULT NULL COMMENT '邮箱',
  `yh_tx` varchar(100) DEFAULT NULL COMMENT '头像',
  `yh_jb` varchar(100) DEFAULT NULL COMMENT '用户级别',
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户账户表';

运行效果
浏览器输入
http://localhost:8888/mall_user
跳转到欢迎页面


浏览器页面输入打开,得到
http://localhost:8888/mall_user/loginRest/login?yh_mch=lilei&yh_mm=1

数据页面



SoapUI的使用

进入bin目录,双击soapui.bat运行软件



鼠标移到Projects,右击打开new soapUI Project



第二行输入
http://192.168.43.54:8888/mall_user/ws?wsdl
点击ok





双击Request1,显示


修改问号参数,例如:你好
点击左上角绿色提交按钮,结果后台显示成功调用



后台


同样新建project
输入

http://192.168.43.54:8888/mall_user/login?wsdl
修改参数,对login接口进行测试
效果



 



在另一个系统进行调用接口的大概步骤
首先,导入相应的cxf依赖包

<!-- 5.CXF -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>3.0.5</version>
		</dependency>
		
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
			<version>3.0.5</version>
		</dependency>
		
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-ws-security</artifactId>
			<version>3.0.5</version>
		</dependency>
		
		<dependency>
			<groupId>org.apache.ws.security</groupId>
			<artifactId>wss4j</artifactId>
			<version>1.5.2</version>
		</dependency>
		
		<!-- 6.rest -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxrs</artifactId>
			<version>3.0.5</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http-jetty</artifactId>
			<version>3.0.5</version>
		</dependency>


 创建同包名同接口名的接口,接口方法跟之前的一样



实现方法

@RequestMapping("login")
	public String goto_login(
			HttpServletResponse response, HttpSession session, T_MALL_USER_ACCOUNT user, HttpServletRequest request,
			ModelMap map) {

		T_MALL_USER_ACCOUNT select_user = new T_MALL_USER_ACCOUNT(); 
				
		// 登陆,远程用户认证接口
		JaxWsProxyFactoryBean jwfb = new JaxWsProxyFactoryBean();
		jwfb.setAddress(MyPropertyUtil.getProperty("ws.properties", "login_url"));
		jwfb.setServiceClass(LoginServerInf.class);
		LoginServerInf create = (LoginServerInf)jwfb.create();
		String loginJson = create.login(user);
		select_user = MyJsonUtil.json_to_object(loginJson, T_MALL_USER_ACCOUNT.class);
		
		if (select_user == null) {
			return "redirect:/login.do";
		} else {
			session.setAttribute("user", select_user);

		return "redirect:/index.do";
        }
	}

ws.properties
 

login_url=http://localhost:8888/mall_user/login?wsdl

工具类
MyJsonUtil.java

package com.atguigu.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.atguigu.bean.T_MALL_SHOPPINGCAR;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import net.sf.json.JSONArray;

public class MyJsonUtil {

	public static void main(String[] args) throws UnsupportedEncodingException {
		List<T_MALL_SHOPPINGCAR> list_car = new ArrayList<T_MALL_SHOPPINGCAR>();

		for (int i = 0; i < 3; i++) {
			T_MALL_SHOPPINGCAR t_MALL_SHOPPINGCAR = new T_MALL_SHOPPINGCAR();
			t_MALL_SHOPPINGCAR.setSku_mch("商品" + i);
			t_MALL_SHOPPINGCAR.setSku_jg(i * 1000);
			list_car.add(t_MALL_SHOPPINGCAR);
		}

	}

	public static <T> String object_to_json(T t) {
		Gson gson = new Gson();

		String json = gson.toJson(t);

		try {
			json = URLEncoder.encode(json, "utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return json;
	}

	public static <T> T json_to_object(String json, Class<T> t) {

		try {
			json = URLDecoder.decode(json, "utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Gson gson = new Gson();

		T fromJson = gson.fromJson(json, t);

		return fromJson;
	}

	public static <T> List<T> json_to_list(String json, Class<T> t) {
		String decode = "";

		if (StringUtils.isBlank(json)) {
			return null;
		} else {
			try {
				decode = URLDecoder.decode(json, "utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			JSONArray fromObject2 = JSONArray.fromObject(decode);

			List<T> list_array = (List<T>) JSONArray.toCollection(fromObject2, t);

			return list_array;
		}

	}

	public static <T> String list_to_json(List<T> list) {

		Gson gson = new Gson();
		String json = gson.toJson(list);

		try {
			json = URLEncoder.encode(json, "utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return json;

	}

}


MyPropertyUtil.java

package com.atguigu.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class MyPropertyUtil {

	public static String getProperty(String pro, String key) {
		Properties properties = new Properties();
		InputStream resourceAsStream = MyPropertyUtil.class.getClassLoader().getResourceAsStream(pro);
		try {
			properties.load(resourceAsStream);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String property = properties.getProperty(key);
		return property;
	}
}

over~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值