Spring Boot調用Web Service接口

一、Spring Boot集成WebService

Web Service是一種跨平台、跨語言的遠程調用技術,它實現了不同系統之間的互操作。

Spring Boot可以通過一個叫做JAX-WS的標準API來調用Web Service接口。

下面是一個簡單的示例代碼,在Spring Boot中引入JAX-WS依賴,並通過@WebServiceClient註解指定要調用的Web Service接口地址及命名空間:

@Configuration
public class WebServiceConfig {

    @Bean(name="helloWorld")
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema helloWorldSchema){
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("HelloWorldPort");
        wsdl11Definition.setTargetNamespace("http://spring.io/guides/gs-producing-web-service");
        wsdl11Definition.setLocationUri("/ws");
        wsdl11Definition.setSchema(helloWorldSchema);
        return wsdl11Definition;
    }

    @Bean
    public XsdSchema helloWorldSchema() {
        return new SimpleXsdSchema(new ClassPathResource("xsd/helloworld.xsd"));
    }

}

在Controller中通過@Autowired注入要調用的Web Service接口,然後就可以愉快地調用了:

@RestController
@RequestMapping("/api")
public class HelloWorldController {

    @Autowired
    private HelloWorldPortType helloWorldPortType;

    @GetMapping("/hello")
    public String sayHello() {
        String response = helloWorldPortType.sayHello("World");
        return response;
    }

}

二、指定Web Service接口方法

在一個Web Service接口中可能會有多個方法,我們可以通過@SOAPBinding註解的參數來指定要調用的方法:

@WebService(targetNamespace="http://spring.io/guides/gs-producing-web-service", name="HelloWorld")
@SOAPBinding(style=Style.RPC)
public interface HelloWorldPortType {

    @WebMethod(operationName="sayHello")
    String sayHello(@WebParam(name="arg0") String arg0);

    @WebMethod(operationName="getHelloWorldAsString")
    String getHelloWorldAsString();

}

注意上面的Web Service接口的註解,其中我們通過@SOAPBinding註解的style參數來指定為RPC模式。

三、傳遞複雜類型參數

有時候我們需要調用一個Web Service接口方法,需要傳遞一個複雜類型的參數。比如下面的Account類:

public class Account {

    private String accountNumber;

    private String accountHolderName;

    private String branch;

    private String location;

    // getter & setter

}

在Web Service接口中我們可以通過@XmlRootElement註解將Account類轉換為XML格式:

@XmlRootElement
public class Account {

    private String accountNumber;

    private String accountHolderName;

    private String branch;

    private String location;

    // getter & setter

}

然後將Account類作為Web Service方法的參數:

public interface Bank {

    @WebMethod(operationName="getAccountDetails")
    AccountDetails getAccountDetails(@WebParam(name="account") Account account);

}

在調用Web Service接口方法時,我們同樣需要將Account對象轉換為XML格式。Spring Boot提供了一個JAXB2 Marshaller來完成這個任務:

@Configuration
public class WebServiceConfig {

    @Bean
    public Jaxb2Marshaller marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("com.example.wsdl.demo");
        return marshaller;
    }

    @Bean
    public SoapClient soapClient(Jaxb2Marshaller marshaller) {
        SoapClient client = new SoapClient();
        client.setDefaultUri("http://localhost:8080/ws");
        client.setMarshaller(marshaller);
        client.setUnmarshaller(marshaller);
        return client;
    }

}

然後就可以愉快地調用Web Service接口方法了:

@Service
public class BankService {

    private final SoapClient soapClient;

    @Autowired
    public BankService(SoapClient soapClient) {
        this.soapClient = soapClient;
    }

    public AccountDetails getAccountDetails(String accountNumber) {
        Account account = new Account();
        account.setAccountNumber(accountNumber);
        AccountDetails accountDetails = (AccountDetails) soapClient.callWebService("http://spring.io/guides/gs-producing-web-service", account);
        return accountDetails;
    }

}

四、使用Cxf客戶端

除了上文提到的JAX-WS API外,Spring Boot還可以使用Apache CXF這個強大的Web Service框架來調用Web Service接口。

我們可以通過Maven引入如下兩個依賴:

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
    <version>3.2.5</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.2.5</version>
</dependency>

然後就可以愉快地使用CXF客戶端來調用Web Service接口了:

@Service
public class BankService {

    private final Bank bank;

    @Autowired
    public BankService(JaxWsProxyFactoryBean proxyFactoryBean) {
        this.bank = proxyFactoryBean.create(Bank.class);
    }

    public AccountDetails getAccountDetails(String accountNumber) {
        Account account = new Account();
        account.setAccountNumber(accountNumber);
        AccountDetails accountDetails = bank.getAccountDetails(account);
        return accountDetails;
    }

}

可以看到,在CXF客戶端中我們可以直接通過Bank類來調用Web Service接口方法。

原創文章,作者:HKKN,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/135474.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
HKKN的頭像HKKN
上一篇 2024-10-04 00:13
下一篇 2024-10-04 00:13

相關推薦

發表回復

登錄後才能評論