开发工具
1. IntelliJ IDEA
2. Maven 3.0+
3. 测试WebService工具,如SOAPUI
开始开发
1.新建Maven工程,在pom.xml
org.springframework.boot
spring-boot-starter-parent
1.5.8.RELEASE
jar
org.springframework.boot
spring-boot-starter-web-services
wsdl4j
wsdl4j
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-jdbc
com.oracle
ojdbc6
11.2.0.3
1.8
org.springframework.boot
spring-boot-maven-plugin
org.codehaus.mojo
jaxb2-maven-plugin
1.6
xjc
xjc
${project.basedir}/src/main/resources/
${project.basedir}/src/main/java
false
2.添加配置application.yml
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
5.因为我们需要对数据库表进行操作,所以,需要对生成的Country类进行修改,从而可以映射到数据库表
@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
@Repository
public interface InfCountryRepository extends JpaRepository{
Country findCountriesByCapital(String capital);
}
在这里我自定义了一个查询方法
7.实现改接口
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 findCountries() {
List 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.实现对请求的处理逻辑
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;
}
}
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;
}
}
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();
}
}
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配置类
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.最后加入程序主类
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
源文件