SpringBoot整合Webservice 提供服務接口 - 服務端


 

引入webservice需要使用jar包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
    <version>3.2.7</version>
</dependency>

書寫接口和實現類

接口類

package org.andot.webservice.demo.server;

import org.andot.webservice.demo.entity.Person;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService(targetNamespace = "http://andot.org/webservice/demo/server")
public interface TestApiService {
    @WebMethod
    Person insertPersonInfo(@WebParam(name = "PERSON", targetNamespace = "http://andot.org/webservice/demo/server") String person);
}

接口實現類

package org.andot.webservice.demo.server.provider;

import com.alibaba.fastjson.JSONArray;
import org.andot.webservice.demo.entity.Person;
import org.andot.webservice.demo.server.TestApiService;
import org.springframework.stereotype.Component;

import javax.jws.WebService;
import java.util.List;

@Component
@WebService(name = "testApiService",
    targetNamespace = "http://andot.org/webservice/demo/server",
    endpointInterface = "org.andot.webservice.demo.server.TestApiService",
    portName = "10000")
public class TestApiServiceImpl implements TestApiService {
    @Override
    public Person insertPersonInfo(String person) {
        List<Person> list = JSONArray.parseArray(person, Person.class);
        //TODO 邏輯處理
        return list.get(0);
    }
}

實體類

package org.andot.webservice.demo.entity;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Person {
    private Integer id;
    private String name;
    private String niceName;
    private Integer age;
    private Double height;
}

配置類

package org.andot.webservice.demo.start;

import org.andot.webservice.demo.server.TestApiService;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

@Configuration
public class TestWebService {
    @Bean
    public ServletRegistrationBean wsServlet(){
        return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
    }

    @Autowired
    private TestApiService testApiService;

    @Autowired
    @Qualifier(Bus.DEFAULT_BUS_ID)
    private SpringBus bus;

    @Bean
    public Endpoint endpoint(){
        EndpointImpl endpoint = new EndpointImpl(bus, testApiService);
        endpoint.publish("/testApiService");
        return endpoint;
    }
}

訪問地址

返回XML

使用POSTman進行測試

地址輸入 

請求方式 POST

請求頭 Context-Type:text/html

請求文本:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <insertPersonInfo xmlns="http://andot.org/webservice/demo/server">
      <PERSON>
        [{"name":"你好"},{"name":"你好2"}]
      </PERSON>
    </insertPersonInfo>
  </soap:Body>
</soap:Envelope>

詳細說明:

(1)insertPersonInfo是方法名。

(2)PERSON是方法參數。

 

java使用SOAP協議訪問webservice接口 xml轉換

https://blog.csdn.net/ycg_x_y/article/details/107927699

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM