Springboot에서 Webservice 통합하기

웹 서비스 개요

웹 서비스란?

웹 서비스는 프로그래밍 언어와 운영 체제 플랫폼을 초월한 원격 호출 기술입니다. 이 기술은 서비스 간의 상호 호출이 개발 언어에 관계없이 이루어집니다.

웹 서비스 플랫폼 기술

  • XML + XSD: 데이터 전송을 위해 XML 형식으로 데이터를 캡슐화합니다.
  • SOAP: HTTP 요청 및 응답에서 XML 형식을 사용하며 특정 HTTP 메시지 헤더를 추가합니다. SOAP은 웹 서비스 호출을 위한 표준 RPC 방식을 제공합니다.
  • WSDL: XML 기반 언어로, 웹 서비스와 그 함수, 매개변수, 반환 값을 설명합니다. 클라이언트와 서버 모두가 이해할 수 있는 표준 형식입니다.

서비스 제공 방법

방법 1: spring-boot-starter-web-services

pom.xml 의존성 설정


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

인터페이스 및 구현


import javax.jws.WebService;

@WebService
public interface SampleService {
    String hello();
}

@Slf4j
@WebService(endpointInterface = "com.example.service.SampleService")
public class SampleServiceImpl implements SampleService {
    @Override
    public String hello() {
        log.info("SampleServiceImpl의 hello 메서드 호출!");
        return "안녕하세요!";
    }
}

엔드포인트 구성


@Configuration
public class WebServiceConfig {

    @Bean
    public Endpoint sampleEndpoint() {
        SampleService service = new SampleServiceImpl();
        return Endpoint.publish("http://localhost:8080/sample", service);
    }
}

접속 주소


http://localhost:8080/sample?wsdl

maven 컴파일 오류 해결

프로그램 패키지 `com.sun.xml.internal.ws.spi` 가 존재하지 않는 경우:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <compilerArguments>
            <bootclasspath>${JAVA_HOME}/jre/lib/rt.jar</bootclasspath>
        </compilerArguments>
    </configuration>
</plugin>
또는 아래 의존성을 추가합니다:

<dependency>
    <groupId>com.sun.xml.ws</groupId>
    <artifactId>jaxws-rt</artifactId>
    <version>2.3.6</version>
</dependency>

방법 2: cxf-spring-boot-starter-jaxws

의존성 추가


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

인터페이스 정의


@Service
@WebService(name = "PatientService",
        targetNamespace = "http://example.com/patient")
public interface PatientWebService {
    @WebMethod
    String getPatientInfo(@WebParam String id);
}

인터페이스 구현


@Slf4j
@Service
@WebService(name = "PatientService",
        targetNamespace = "http://example.com/patient",
        endpointInterface = "com.example.webservice.PatientWebService")
public class PatientWebServiceImpl implements PatientWebService {
    @Override
    public String getPatientInfo(String id) {
        log.info("환자 ID: {}", id);
        if (id == null || id.isEmpty()) {
            return "잘못된 입력";
        }
        return String.format("환자 %s 정보 조회 성공!", id);
    }
}

CXF 설정


@Configuration
public class CxfConfiguration {

    private final Bus bus;
    private final PatientWebService patientWebService;

    public CxfConfiguration(Bus bus, PatientWebService patientWebService) {
        this.bus = bus;
        this.patientWebService = patientWebService;
    }

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

    @Bean
    public ServletRegistrationBean<CXFServlet> cxfServlet() {
        return new ServletRegistrationBean<>(new CXFServlet(), "/webservice/*");
    }
}

서비스 테스트

웹 브라우저나 soapUI를 통해 다음 주소로 접속하여 테스트합니다:

http://localhost:8080/webservice/PatientService?wsdl

태그: SpringBoot JAX-WS CXF

5월 30일 16:07에 게시됨