基于SpringBoot使用Spring jpa实现对Oracle表的增、删、改、查的Spring WebService


概述
近来,学习有关WebService的内容,应业务需求探索下关于Spring WebService知识,需要快速生成对数据库表的一些操作,然后其他系统调用,和一般的大型网站一样,为第三方生成有关的服务。找了下网上,没有比较全面具体的文章介绍关于这方面的内容。自己逛了下Spring社区找了些资料和自己学习总结,进行了一番探索,完成了一个关于这方面的demo,虽然只是实现了简单的对数据库的增删改,但是个人认为还是比较完整的,可以给这方面有需求的开发者一个抛砖引玉的一个作用,博客中如有不准确的地方,还请交流指正,好了,不多说了,进入正题:


开发工具

1. IntelliJ IDEA

2. Maven 3.0+

3. 测试WebService工具,如SOAPUI


开始开发

1.新建Maven工程,在pom.xml

  1. <parent>  
  2.     <groupId>org.springframework.boot</groupId>  
  3.     <artifactId>spring-boot-starter-parent</artifactId>  
  4.     <version>1.5.8.RELEASE</version>  
  5. </parent>  
  6.   
  7. <packaging>jar</packaging>  
  8.   
  9. <dependencies>  
  10.     <dependency>  
  11.         <groupId>org.springframework.boot</groupId>  
  12.         <artifactId>spring-boot-starter-web-services</artifactId>  
  13.     </dependency>  
  14.   
  15.     <dependency>  
  16.         <groupId>wsdl4j</groupId>  
  17.         <artifactId>wsdl4j</artifactId>  
  18.     </dependency>  
  19.   
  20.     <dependency>  
  21.         <groupId>org.springframework.boot</groupId>  
  22.         <artifactId>spring-boot-starter-data-jpa</artifactId>  
  23.     </dependency>  
  24.   
  25.     <dependency>  
  26.         <groupId>org.springframework.boot</groupId>  
  27.         <artifactId>spring-boot-starter-jdbc</artifactId>  
  28.     </dependency>  
  29.   
  30.     <dependency>  
  31.         <groupId>com.oracle</groupId>  
  32.         <artifactId>ojdbc6</artifactId>  
  33.         <version>11.2.0.3</version>  
  34.     </dependency>  
  35. </dependencies>  
  36.   
  37. <properties>  
  38.     <java.version>1.8</java.version>  
  39. </properties>  
  40.   
  41. <build>  
  42.     <plugins>  
  43.         <plugin>  
  44.             <groupId>org.springframework.boot</groupId>  
  45.             <artifactId>spring-boot-maven-plugin</artifactId>  
  46.         </plugin>  
  47.         <plugin>  
  48.             <groupId>org.codehaus.mojo</groupId>  
  49.             <artifactId>jaxb2-maven-plugin</artifactId>  
  50.             <version>1.6</version>  
  51.             <executions>  
  52.                 <execution>  
  53.                     <id>xjc</id>  
  54.                     <goals>  
  55.                         <goal>xjc</goal>  
  56.                     </goals>  
  57.                 </execution>  
  58.             </executions>  
  59.             <configuration>  
  60.                 <schemaDirectory>${project.basedir}/src/main/resources/</schemaDirectory>  
  61.                 <outputDirectory>${project.basedir}/src/main/java</outputDirectory>  
  62.                 <clearOutputDir>false</clearOutputDir>  
  63.             </configuration>  
  64.         </plugin>  
  65.     </plugins>  
  66. </build>  
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.8.RELEASE</version>
</parent>

<packaging>jar</packaging>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web-services</artifactId>
    </dependency>

    <dependency>
        <groupId>wsdl4j</groupId>
        <artifactId>wsdl4j</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.3</version>
    </dependency>
</dependencies>

<properties>
    <java.version>1.8</java.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>1.6</version>
            <executions>
                <execution>
                    <id>xjc</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <schemaDirectory>${project.basedir}/src/main/resources/</schemaDirectory>
                <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
                <clearOutputDir>false</clearOutputDir>
            </configuration>
        </plugin>
    </plugins>
</build>


2.添加配置application.yml

  1. spring:  
  2.   datasource:  
  3.     driver-class-name: oracle.jdbc.driver.OracleDriver  
  4.     url: jdbc:oracle:thin:@localhost:1521:xe  
  5.     username: xxxx  
  6.     password: xxxx  
  7.   jpa:  
  8.     hibernate:  
  9.       ddl-auto: update  
  10.     show-sql: true  
  11.   jackson:  
  12.     serialization: true  
  13. server:  
  14.   port: 9000  
spring:
  datasource:
    driver-class-name: oracle.jdbc.driver.OracleDriver
    url: jdbc:oracle:thin:@localhost:1521:xe
    username: xxxx
    password: xxxx
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
  jackson:
    serialization: true
server:
  port: 9000

3.加入xmlSchame定义WSDL
  1. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.example.com"  
  2.            targetNamespace="http://www.example.com" elementFormDefault="qualified">  
  3.   
  4.     <xs:element name="deleteCountryByCapitalRequest">  
  5.         <xs:complexType>  
  6.             <xs:sequence>  
  7.                 <xs:element name="capital" type="xs:string"/>  
  8.             </xs:sequence>  
  9.         </xs:complexType>  
  10.     </xs:element>  
  11.   
  12.     <xs:element name="deleteCountryByCapitalResponse">  
  13.         <xs:complexType>  
  14.             <xs:sequence>  
  15.                 <xs:element name="result" type="xs:string" fixed="OK"/>  
  16.             </xs:sequence>  
  17.         </xs:complexType>  
  18.     </xs:element>  
  19.   
  20.     <xs:element name="getCountryRequest">  
  21.         <xs:complexType>  
  22.             <xs:sequence>  
  23.                 <xs:element name="name" type="xs:string"/>  
  24.             </xs:sequence>  
  25.         </xs:complexType>  
  26.     </xs:element>  
  27.   
  28.     <xs:element name="getCountryResponse">  
  29.         <xs:complexType>  
  30.             <xs:sequence>  
  31.                 <xs:element name="country" type="tns:country"/>  
  32.             </xs:sequence>  
  33.         </xs:complexType>  
  34.     </xs:element>  
  35.   
  36.     <xs:element name="getCountriesRequest">  
  37.         <xs:complexType>  
  38.             <xs:sequence>  
  39.             </xs:sequence>  
  40.         </xs:complexType>  
  41.     </xs:element>  
  42.   
  43.     <xs:element name="getCountriesResponse">  
  44.         <xs:complexType>  
  45.             <xs:sequence>  
  46.                 <xs:element name="country" type="tns:country"/>  
  47.             </xs:sequence>  
  48.         </xs:complexType>  
  49.     </xs:element>  
  50.   
  51.     <xs:element name="createCountryRequest">  
  52.         <xs:complexType>  
  53.             <xs:sequence>  
  54.                 <xs:element name="name" type="xs:string"/>  
  55.                 <xs:element name="population" type="xs:int"/>  
  56.                 <xs:element name="capital" type="xs:string"/>  
  57.                 <xs:element name="currency" type="tns:currency"/>  
  58.             </xs:sequence>  
  59.         </xs:complexType>  
  60.     </xs:element>  
  61.   
  62.     <xs:element name="createCountryResponse">  
  63.         <xs:complexType>  
  64.             <xs:sequence>  
  65.                 <xs:element name="result" type="xs:string" fixed="OK"/>  
  66.             </xs:sequence>  
  67.         </xs:complexType>  
  68.     </xs:element>  
  69.   
  70.     <xs:complexType name="country">  
  71.         <xs:sequence>  
  72.             <xs:element name="name" type="xs:string"/>  
  73.             <xs:element name="population" type="xs:int"/>  
  74.             <xs:element name="capital" type="xs:string"/>  
  75.             <xs:element name="currency" type="tns:currency"/>  
  76.         </xs:sequence>  
  77.     </xs:complexType>  
  78.   
  79.     <xs:simpleType name="currency">  
  80.         <xs:restriction base="xs:string">  
  81.             <xs:enumeration value="GBP"/>  
  82.             <xs:enumeration value="EUR"/>  
  83.             <xs:enumeration value="PLN"/>  
  84.             <xs:enumeration value="RMB"/>  
  85.             <xs:enumeration value="JPY"/>  
  86.         </xs:restriction>  
  87.     </xs:simpleType>  
  88. </xs:schema>  
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.example.com"
           targetNamespace="http://www.example.com" elementFormDefault="qualified">

    <xs:element name="deleteCountryByCapitalRequest">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="capital" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="deleteCountryByCapitalResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="result" type="xs:string" fixed="OK"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="getCountryRequest">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="getCountryResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="country" type="tns:country"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="getCountriesRequest">
        <xs:complexType>
            <xs:sequence>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="getCountriesResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="country" type="tns:country"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="createCountryRequest">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="population" type="xs:int"/>
                <xs:element name="capital" type="xs:string"/>
                <xs:element name="currency" type="tns:currency"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="createCountryResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="result" type="xs:string" fixed="OK"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="country">
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="population" type="xs:int"/>
            <xs:element name="capital" type="xs:string"/>
            <xs:element name="currency" type="tns:currency"/>
        </xs:sequence>
    </xs:complexType>

    <xs:simpleType name="currency">
        <xs:restriction base="xs:string">
            <xs:enumeration value="GBP"/>
            <xs:enumeration value="EUR"/>
            <xs:enumeration value="PLN"/>
            <xs:enumeration value="RMB"/>
            <xs:enumeration value="JPY"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

4.使用IDE,有xsd自动生成Java Class



5.因为我们需要对数据库表进行操作,所以,需要对生成的Country类进行修改,从而可以映射到数据库表

  1. @Id  
  2.    @Column  
  3.    @XmlElement(namespace = "http://www.example.com"required = true)  
  4.    protected String name;  
  5.    @Column  
  6.    @XmlElement(namespace = "http://www.example.com")  
  7.    protected int population;  
  8.    @Column  
  9.    @XmlElement(namespace = "http://www.example.com"required = true)  
  10.    protected String capital;  
  11.    @Column  
  12.    @XmlElement(namespace = "http://www.example.com"required = true)  
  13.    @XmlSchemaType(name = "currency")  
  14.    protected Currency currency;  
 @Id
    @Column
    @XmlElement(namespace = "http://www.example.com", required = true)
    protected String name;
    @Column
    @XmlElement(namespace = "http://www.example.com")
    protected int population;
    @Column
    @XmlElement(namespace = "http://www.example.com", required = true)
    protected String capital;
    @Column
    @XmlElement(namespace = "http://www.example.com", required = true)
    @XmlSchemaType(name = "currency")
    protected Currency currency;


接下来是jpa的操作,熟悉jpa的朋友可以跳过6,7


6.新建接口继承JpaRepository

  1. @Repository  
  2. public interface InfCountryRepository extends JpaRepository<Country,String>{  
  3.   
  4.     Country findCountriesByCapital(String capital);  
  5. }  
@Repository
public interface InfCountryRepository extends JpaRepository<Country,String>{

    Country findCountriesByCapital(String capital);
}

在这里我自定义了一个查询方法


7.实现改接口

  1. import com.example.entity.Country;  
  2. import org.springframework.beans.factory.annotation.Autowired;  
  3. import org.springframework.stereotype.Component;  
  4. import org.springframework.util.Assert;  
  5.   
  6. import java.util.List;  
  7.   
  8.   
  9. /*******************Copyright Information************************ 
  10.  *              AUTHOR: Lorin.Mitchell                           * 
  11.  *              DATE: 2017/11/6                                 * 
  12.  *              TIME: 19:05                                      *  
  13.  ****************************************************************/  
  14.   
  15. @Component  
  16. public class CountryRepository {  
  17.   
  18.     @Autowired  
  19.     private InfCountryRepository repository;  
  20.   
  21.     public Country findCountry(String name){  
  22.         Assert.notNull(name, "The country's name must not be null");  
  23.         Country country = this.repository.findOne(name);  
  24.         return country;  
  25.     }  
  26.   
  27.     public List<Country> findCountries() {  
  28.         List<Country> list = this.repository.findAll();  
  29.         return list;  
  30.     }  
  31.   
  32.     public void save(Country country){  
  33.         repository.save(country);  
  34.     }  
  35.   
  36.     public void deleteByCap(String cap){  
  37.         repository.delete(repository.findCountriesByCapital(cap));  
  38.     }  
  39. }  
import com.example.entity.Country;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.List;


/*******************Copyright Information************************
 *              AUTHOR: Lorin.Mitchell                           *
 *              DATE: 2017/11/6                                 *
 *              TIME: 19:05                                      * 
 ****************************************************************/

@Component
public class CountryRepository {

    @Autowired
    private InfCountryRepository repository;

    public Country findCountry(String name){
        Assert.notNull(name, "The country's name must not be null");
        Country country = this.repository.findOne(name);
        return country;
    }

    public List<Country> findCountries() {
        List<Country> list = this.repository.findAll();
        return list;
    }

    public void save(Country country){
        repository.save(country);
    }

    public void deleteByCap(String cap){
        repository.delete(repository.findCountriesByCapital(cap));
    }
}


8.实现对请求的处理逻辑

  1. import com.example.dao.CountryRepository;  
  2. import com.example.entity.GetCountriesRequest;  
  3. import com.example.entity.GetCountriesResponse;  
  4. import org.springframework.beans.factory.annotation.Autowired;  
  5. import org.springframework.ws.server.endpoint.annotation.Endpoint;  
  6. import org.springframework.ws.server.endpoint.annotation.PayloadRoot;  
  7. import org.springframework.ws.server.endpoint.annotation.RequestPayload;  
  8. import org.springframework.ws.server.endpoint.annotation.ResponsePayload;  
  9.   
  10. /*******************Copyright Information************************ 
  11.  *              AUTHOR: Lorin.Mitchell                           * 
  12.  *              DATE: 2017/11/7                                 * 
  13.  *              TIME: 19:27                                      * 
  14.  ****************************************************************/  
  15. @Endpoint  
  16. public class CountriesEndpoint {  
  17.     private static final String NAMESPACE_URI = "http://www.example.com";  
  18.   
  19.     @Autowired  
  20.     private CountryRepository countryRepository;  
  21.   
  22.         @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountriesRequest")  
  23.     @ResponsePayload  
  24.     public GetCountriesResponse getCountries(@RequestPayload GetCountriesRequest request) {  
  25.         GetCountriesResponse response = new GetCountriesResponse();  
  26.         response.setCountries(countryRepository.findCountries());  
  27.   
  28.         return response;  
  29.     }  
  30. }  
import com.example.dao.CountryRepository;
import com.example.entity.GetCountriesRequest;
import com.example.entity.GetCountriesResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

/*******************Copyright Information************************
 *              AUTHOR: Lorin.Mitchell                           *
 *              DATE: 2017/11/7                                 *
 *              TIME: 19:27                                      *
 ****************************************************************/
@Endpoint
public class CountriesEndpoint {
    private static final String NAMESPACE_URI = "http://www.example.com";

    @Autowired
    private CountryRepository countryRepository;

        @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountriesRequest")
    @ResponsePayload
    public GetCountriesResponse getCountries(@RequestPayload GetCountriesRequest request) {
        GetCountriesResponse response = new GetCountriesResponse();
        response.setCountries(countryRepository.findCountries());

        return response;
    }
}

这里xsd定义了4个请求,处理方式和上述方式类似

  1. import com.example.dao.CountryRepository;  
  2. import com.example.entity.GetCountryRequest;  
  3. import com.example.entity.GetCountryResponse;  
  4. import org.springframework.beans.factory.annotation.Autowired;  
  5. import org.springframework.ws.server.endpoint.annotation.Endpoint;  
  6. import org.springframework.ws.server.endpoint.annotation.PayloadRoot;  
  7. import org.springframework.ws.server.endpoint.annotation.RequestPayload;  
  8. import org.springframework.ws.server.endpoint.annotation.ResponsePayload;  
  9.   
  10. /*******************Copyright Information************************  
  11.  *              AUTHOR: Lorin.Mitchell                           *  
  12.  *              DATE: 2017/11/6                                 *  
  13.  *              TIME: 19:39                                      *   
  14.  ****************************************************************/  
  15.     //@Endpoint注解将该类标注为SOA的传入信息类  
  16. @Endpoint  
  17. public class CountryEndpoint {  
  18.     private static final String NAMESPACE_URI = "http://www.example.com";  
  19.   
  20.     @Autowired  
  21.     private CountryRepository countryRepository;  
  22.   
  23.   
  24.     @PayloadRoot(namespace = NAMESPACE_URIlocalPart = "getCountryRequest")  
  25.     @ResponsePayload  
  26.     public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request) {  
  27.         GetCountryResponse response = new GetCountryResponse();  
  28.         response.setCountry(countryRepository.findCountry(request.getName()));  
  29.   
  30.         return response;  
  31.     }  
  32.   
  33. }  
import com.example.dao.CountryRepository;
import com.example.entity.GetCountryRequest;
import com.example.entity.GetCountryResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

/*******************Copyright Information************************
 *              AUTHOR: Lorin.Mitchell                           *
 *              DATE: 2017/11/6                                 *
 *              TIME: 19:39                                      * 
 ****************************************************************/
    //@Endpoint注解将该类标注为SOA的传入信息类
@Endpoint
public class CountryEndpoint {
    private static final String NAMESPACE_URI = "http://www.example.com";

    @Autowired
    private CountryRepository countryRepository;


    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountryRequest")
    @ResponsePayload
    public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request) {
        GetCountryResponse response = new GetCountryResponse();
        response.setCountry(countryRepository.findCountry(request.getName()));

        return response;
    }

}

  1. import org.springframework.beans.factory.annotation.Autowired;  
  2. import org.springframework.ws.server.endpoint.annotation.Endpoint;  
  3. import org.springframework.ws.server.endpoint.annotation.PayloadRoot;  
  4. import org.springframework.ws.server.endpoint.annotation.RequestPayload;  
  5. import org.springframework.ws.server.endpoint.annotation.ResponsePayload;  
  6.   
  7. /*******************Copyright Information************************ 
  8.  *              AUTHOR: Lorin.Mitchell                           * 
  9.  *              DATE: 2017/11/8                                 * 
  10.  *              TIME: 14:14                                      *  
  11.  ****************************************************************/  
  12. @Endpoint  
  13. public class CreateCountryEndpoint {  
  14.     private static final String NAMESPACE_URI = "http://wwwexample.com";  
  15.   
  16.     @Autowired  
  17.     private CountryRepository countryRepository;  
  18.   
  19.     //@PayloadRoot注解,表示该方法将处理以namespace和localPart的xml请求信息  
  20.     @PayloadRoot(namespace = NAMESPACE_URI, localPart = "createCountryRequest")  
  21.     //@ResponsePayload注解,表示将返回值映射到reponse中  
  22.     @ResponsePayload  
  23.     public CreateCountryResponse createCountryResponse(@RequestPayload CreateCountryRequest request){  
  24.         Country country = new Country();  
  25.         country.setName(request.getName());  
  26.         country.setCapital(request.getCapital());  
  27.         country.setCurrency(request.getCurrency());  
  28.         country.setPopulation(request.getPopulation());  
  29.         countryRepository.save(country);  
  30.         return new CreateCountryResponse();  
  31.     }  
  32. }  
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

/*******************Copyright Information************************
 *              AUTHOR: Lorin.Mitchell                           *
 *              DATE: 2017/11/8                                 *
 *              TIME: 14:14                                      * 
 ****************************************************************/
@Endpoint
public class CreateCountryEndpoint {
    private static final String NAMESPACE_URI = "http://wwwexample.com";

    @Autowired
    private CountryRepository countryRepository;

    //@PayloadRoot注解,表示该方法将处理以namespace和localPart的xml请求信息
    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "createCountryRequest")
    //@ResponsePayload注解,表示将返回值映射到reponse中
    @ResponsePayload
    public CreateCountryResponse createCountryResponse(@RequestPayload CreateCountryRequest request){
        Country country = new Country();
        country.setName(request.getName());
        country.setCapital(request.getCapital());
        country.setCurrency(request.getCurrency());
        country.setPopulation(request.getPopulation());
        countryRepository.save(country);
        return new CreateCountryResponse();
    }
}

  1. import org.springframework.beans.factory.annotation.Autowired;  
  2. import org.springframework.ws.server.endpoint.annotation.Endpoint;  
  3. import org.springframework.ws.server.endpoint.annotation.PayloadRoot;  
  4. import org.springframework.ws.server.endpoint.annotation.RequestPayload;  
  5. import org.springframework.ws.server.endpoint.annotation.ResponsePayload;  
  6.   
  7. /*******************Copyright Information************************ 
  8.  *              AUTHOR: Lorin.Mitchell                           * 
  9.  *              DATE: 2017/11/8                                 * 
  10.  *              TIME: 15:25                                      *  
  11.  ****************************************************************/  
  12. @Endpoint  
  13. public class DeleteCountryByCapEndpoint {  
  14.     private static final String NAMESPACE_URI = "http://www.example.com";  
  15.   
  16.     @Autowired  
  17.     private CountryRepository countryRepository;  
  18.   
  19.   
  20.     //@PayloadRoot注解,表示该方法将处理以namespace和localPart的xml请求信息  
  21.     @PayloadRoot(namespace = NAMESPACE_URI, localPart = "deleteCountryByCapitalRequest")  
  22.     //@ResponsePayload注解,表示将返回值映射到reponse中  
  23.     @ResponsePayload  
  24.     public DeleteCountryByCapitalResponse deleteCountryByCapitalResponse(  
  25.             @RequestPayload DeleteCountryByCapitalRequest request) {  
  26.        countryRepository.deleteByCap(request.getCapital());  
  27.        return new DeleteCountryByCapitalResponse();  
  28.     }  
  29. }  
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

/*******************Copyright Information************************
 *              AUTHOR: Lorin.Mitchell                           *
 *              DATE: 2017/11/8                                 *
 *              TIME: 15:25                                      * 
 ****************************************************************/
@Endpoint
public class DeleteCountryByCapEndpoint {
    private static final String NAMESPACE_URI = "http://www.example.com";

    @Autowired
    private CountryRepository countryRepository;


    //@PayloadRoot注解,表示该方法将处理以namespace和localPart的xml请求信息
    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "deleteCountryByCapitalRequest")
    //@ResponsePayload注解,表示将返回值映射到reponse中
    @ResponsePayload
    public DeleteCountryByCapitalResponse deleteCountryByCapitalResponse(
            @RequestPayload DeleteCountryByCapitalRequest request) {
       countryRepository.deleteByCap(request.getCapital());
       return new DeleteCountryByCapitalResponse();
    }
}


9.加入ws配置类

  1. import org.springframework.boot.web.servlet.ServletRegistrationBean;  
  2. import org.springframework.context.ApplicationContext;  
  3. import org.springframework.context.annotation.Bean;  
  4. import org.springframework.context.annotation.Configuration;  
  5. import org.springframework.core.io.ClassPathResource;  
  6. import org.springframework.ws.config.annotation.EnableWs;  
  7. import org.springframework.ws.config.annotation.WsConfigurerAdapter;  
  8. import org.springframework.ws.transport.http.MessageDispatcherServlet;  
  9. import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;  
  10. import org.springframework.xml.xsd.SimpleXsdSchema;  
  11. import org.springframework.xml.xsd.XsdSchema;  
  12.   
  13. /*******************Copyright Information************************ 
  14.  *              AUTHOR: Lorin.Mitchell                           * 
  15.  *              DATE: 2017/11/6                                 * 
  16.  *              TIME: 19:48                                      *  
  17.  ****************************************************************/  
  18. @EnableWs  
  19. @Configuration  
  20. public class WebServiceConfig extends WsConfigurerAdapter {  
  21.     @Bean  
  22.     public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {  
  23.         MessageDispatcherServlet servlet = new MessageDispatcherServlet();  
  24.         servlet.setApplicationContext(applicationContext);  
  25.         servlet.setTransformWsdlLocations(true);  
  26.         return new ServletRegistrationBean(servlet, "/webservice/*");  
  27.     }  
  28.   
  29.     @Bean(name = "countries")  
  30.     public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema countriesSchema) {  
  31.         DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();  
  32.         wsdl11Definition.setPortTypeName("CountriesPort");  
  33.         wsdl11Definition.setLocationUri("/ws");  
  34.         wsdl11Definition.setTargetNamespace("http://www.example.com");  
  35.         wsdl11Definition.setSchema(countriesSchema);  
  36.         return wsdl11Definition;  
  37.     }  
  38.   
  39.     @Bean  
  40.     public XsdSchema countriesSchema() {  
  41.         return new SimpleXsdSchema(new ClassPathResource("countries.xsd"));  
  42.     }  
  43. }  
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.config.annotation.WsConfigurerAdapter;
import org.springframework.ws.transport.http.MessageDispatcherServlet;
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
import org.springframework.xml.xsd.SimpleXsdSchema;
import org.springframework.xml.xsd.XsdSchema;

/*******************Copyright Information************************
 *              AUTHOR: Lorin.Mitchell                           *
 *              DATE: 2017/11/6                                 *
 *              TIME: 19:48                                      * 
 ****************************************************************/
@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {
    @Bean
    public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean(servlet, "/webservice/*");
    }

    @Bean(name = "countries")
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema countriesSchema) {
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("CountriesPort");
        wsdl11Definition.setLocationUri("/ws");
        wsdl11Definition.setTargetNamespace("http://www.example.com");
        wsdl11Definition.setSchema(countriesSchema);
        return wsdl11Definition;
    }

    @Bean
    public XsdSchema countriesSchema() {
        return new SimpleXsdSchema(new ClassPathResource("countries.xsd"));
    }
}


10.最后加入程序主类

  1. import org.springframework.boot.SpringApplication;  
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  3.   
  4. /*******************Copyright Information************************ 
  5.  *              AUTHOR: Lorin.Mitchell                           * 
  6.  *              DATE: 2017/11/6                                 * 
  7.  *              TIME: 18:59                                      *  
  8.  ****************************************************************/  
  9. @SpringBootApplication  
  10. public class Application {  
  11.     public static void main(String[] args) {  
  12.         SpringApplication.run(Application.class, args);  
  13.     }  
  14. }  
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/*******************Copyright Information************************
 *              AUTHOR: Lorin.Mitchell                           *
 *              DATE: 2017/11/6                                 *
 *              TIME: 18:59                                      * 
 ****************************************************************/
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}


11.整个Spring WebService开发已经完成了


测试

可以使用SOAPUI进行测试WSDL的地址URL:http://localhost:9000/webservice/countries.wsdl



源文件


欢迎转载,转载请注明出处,谢谢

Lorin.Mitchell

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值