26 Eylül 2021 Pazar

CXF İle SOAP

Giriş
CFX sadece bir kütüphane değil. Yanında bir sürü araçla birlikte geliyor
JAX-RS ve JAX-WS (yani SOAP) için kullanılabilir.

Örnek
Maven ile şöyle yaparız
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
       
<dependency>
  <groupId>org.glassfish.jaxb</groupId>
  <artifactId>jaxb-runtime</artifactId>
</dependency>
       
<dependency>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
  <version>${cxf.version}</version>
</dependency>
<dependency>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-rt-features-logging</artifactId>
  <version>${cxf.version}</version>
</dependency>
Elimizde şöyle bir kod olsun
@WebService
public interface HelloWorldWS {
    @WebMethod
    String createMessage(@WebParam(name = "createMessageRequest", mode = WebParam.Mode.IN)
String name);
}

@Component
public class HelloWorldWSImpl implements HelloWorldWS{
    @Override
    public String createMessage(String name){
        return "Hello "+name;
    }
}
Şöyle yaparız. Spring uygulamasını çalıştırınca "http://localhost:8080/ws/helloWorldWS?wsdl" adresinde WSDL çıktısı görülebilir.
import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import com.cfxconsumer.soapcxfconsumer.ws.HelloWorldWS;

@Configuration
@ImportResource({ "classpath:META-INF/cxf/cxf.xml" })
public class CxfWebServiceConfig {
  @Autowired
  private Bus cxfBus;

  @Bean
  public ServletRegistrationBean cxfServlet() {
    org.apache.cxf.transport.servlet.CXFServlet cxfServlet =
new org.apache.cxf.transport.servlet.CXFServlet();
    ServletRegistrationBean def = new ServletRegistrationBean<>(cxfServlet, "/ws/*");
    def.setLoadOnStartup(1);
    return def;
  }

  @Bean
  public Endpoint helloWorldWebService(HelloWorldWS helloWorldWS) {
    EndpointImpl endpoint = new EndpointImpl(cxfBus, helloWorldWS);
    endpoint.setAddress("/helloWorldWS");
    endpoint.publish();
    return endpoint;
  }
}


Hiç yorum yok:

Yorum Gönder