Verity's Daily Logs_

[SOAP]Web Service - Clien side 생성 본문

Spring Framework/Project

[SOAP]Web Service - Clien side 생성

johye0 2022. 2. 17. 19:42
반응형

 

3. Client access service

3-1. wsdl 다운로드


http://localhost:8088/ws/user?wsdl

  1. 이전 글(Server side 생성)에서 만든 wsdl 주소로 접속해 해당 내용을 user.wsdl 이름으로 저장한다.
  2. 프로젝트 내 resources/wsdl 경로에 파일을 저장한다. (wsdl 폴더 생성 후 저장)
  3. 첫 줄에 아래 문구를 추가한다. (보통 추가되어 있음)
  4. <?xml version="1.0" encoding="UTF-8" standalone="no"?>

3-2. pom.xml - plugin 추가


<plugin>
     <groupId>org.apache.cxf</groupId>
     <artifactId>cxf-codegen-plugin</artifactId>
     <version>3.5.0</version>
     <executions>
         <execution>
             <id>generate-sources</id>
             <phase>generate-sources</phase>
             <configuration>
                 <sourceRoot>${project.build.directory}/generated-sources/cxf</sourceRoot>
                 <wsdlOptions>
                     <wsdlOption>
                         <wsdl>${basedir}/src/main/resources/wsdl/user.wsdl</wsdl>
                     </wsdlOption>
                 </wsdlOptions>
             </configuration>
             <goals>
                 <goal>wsdl2java</goal>
             </goals>
         </execution>
     </executions>
 </plugin>

Maven > Plugins > cxf-codegen > csf-codegen:wsdl2java 실행 및 결과

csf-codegen:wsdl2java 실행
csf-codegen:wsdl2java 실행 결과

 

3-3. Configuration 추가


csf-codegen:wsdl2java 실행으로 얻은 UserService, User를 import 하도록 주의

Server side에서 만든 UserService, User 가 아님을 주의하자. (import 경로 확인 필수)

package com.happy.webservice.cxf.config;

import com.happy.cxf.ws.UserService;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CxfClientConfig {

    @Value("${server.serviceUrl}")
    private String serviceUrl;

    @Bean
    public UserService userService() throws Exception {
        JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
        jaxWsProxyFactoryBean.setServiceClass(UserService.class);
        jaxWsProxyFactoryBean.setAddress(serviceUrl);

        UserService userService = (UserService) jaxWsProxyFactoryBean.create();
        return userService;
    }
}

 

3-4. application.yml 추가


server:
  port: 8088
  serviceUrl: <http://localhost:8088/ws/user>

 

3-5 Facade 클래스 추가


package com.happy.webservice.cxf.service;

import com.happy.cxf.ws.User;
import com.happy.cxf.ws.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserFacadeService {

    @Resource
    private UserService userService;

    public String getUsername(String userId) {
        String userName = userService.getUserName(userId);
        return userName;
    }

    public User getUser(String userId) {
        User user = userService.getUser(userId);
        return user;
    }

    public List<User> getUserList(String userId) {
        List<User> userList = userService.getUserList(userId);
        return userList;
    }
}

 

3-6 테스트용 Controller 추가


package com.happy.webservice.cxf.controller;

import com.happy.cxf.ws.User;
import com.happy.webservice.cxf.service.UserFacadeService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user-client")
public class UserController {

    @Resource
    private UserFacadeService userFacadeService;

    @GetMapping("/get")
    public String getUsername(String userId){
        System.out.println("client#userId:" + userId);
        return userFacadeService.getUsername(userId);
    }

    @GetMapping("/getUser")
    public User getUser(String userId){
        System.out.println("client#userId:" + userId);
        return userFacadeService.getUser(userId);
    }

    @GetMapping("/getUserList")
    public List<User> getUserList(String userId){
        System.out.println("client#userId:" + userId);
        return userFacadeService.getUserList(userId);
    }
}

 

3-7. TEST


http://localhost:8088/user-client/get?userId=1

 

 

 

[참고] https://www.fatalerrors.org/a/spring-boot-quickly-integrates-cxf.html

반응형
Comments