<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Asrat77</title>
    <description>The latest articles on DEV Community by Asrat77 (@asrat77).</description>
    <link>https://dev.to/asrat77</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1794380%2F395fbf31-a621-4e55-95f8-127cb199f081.jpeg</url>
      <title>DEV Community: Asrat77</title>
      <link>https://dev.to/asrat77</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/asrat77"/>
    <language>en</language>
    <item>
      <title>Consuming and Producing SOAP Services with Spring Boot</title>
      <dc:creator>Asrat77</dc:creator>
      <pubDate>Tue, 23 Jul 2024 08:47:26 +0000</pubDate>
      <link>https://dev.to/asrat77/consuming-and-producing-soap-services-with-spring-boot-58mf</link>
      <guid>https://dev.to/asrat77/consuming-and-producing-soap-services-with-spring-boot-58mf</guid>
      <description>&lt;h4&gt;
  
  
  Setting Up Your Spring Boot Project
&lt;/h4&gt;

&lt;p&gt;To get started, head over to Spring Initializr. Here, you can quickly generate a Spring Boot project with your required dependencies. Configure your project with the following settings:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Project: Maven&lt;/li&gt;
&lt;li&gt;Language: Java&lt;/li&gt;
&lt;li&gt;Spring Boot: 3.3.0&lt;/li&gt;
&lt;li&gt;Java Version: 17&lt;/li&gt;
&lt;li&gt;Dependencies:

&lt;ul&gt;
&lt;li&gt;Spring Data JPA (Java Persistence API)&lt;/li&gt;
&lt;li&gt;Spring Web&lt;/li&gt;
&lt;li&gt;Spring Web Services&lt;/li&gt;
&lt;li&gt;PostgreSQL&lt;/li&gt;
&lt;li&gt;WSDL (Web Service Definition Language)&lt;/li&gt;
&lt;li&gt;Jakarta Persistence (if required)
#### Understanding Inversion of Control (IoC) and Dependency Injection (DI)
Inversion of Control (IoC) is a fundamental principle of Spring. Instead of the application calling the framework, the framework calls the components of the application. This is achieved through Dependency Injection (DI), where the framework injects dependencies into classes rather than the classes creating dependencies themselves. This promotes loose coupling and enhances testability.
#### Step 1: Creating the Model
Define a model class that represents the data structure of your application. This class should implement the Serializable interface to facilitate object serialization and deserialization.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Table(name="client")
public class Client implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="id")
    private long id;

    // Repeat for other attributes

    // Getters and Setters
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Description: The Client class represents a table in your database. By marking it with @Entity and @Table, you make it a managed entity in JPA.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 2: Creating the Repository Layer
&lt;/h4&gt;

&lt;p&gt;Create an interface that extends JpaRepository to provide CRUD operations for your model.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;```import com.example.client_mgmt.model.Client;&lt;br&gt;
import org.springframework.data.jpa.repository.JpaRepository;&lt;br&gt;
import org.springframework.stereotype.Repository;&lt;/p&gt;

&lt;p&gt;@Repository&lt;br&gt;
public interface ClientRepository extends JpaRepository {&lt;br&gt;
}&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Description: The ClientRepository interface allows you to perform database operations on the Client entity without writing boilerplate code.
#### Step 3: Creating the Service Layer
Define a service interface and its implementation to manage Client entities, encapsulating business logic.


```import com.example.client_mgmt.model.Client;
import java.util.List;
import java.util.Optional;

public interface ClientService {
    void addClient(Client client);
    Optional&amp;lt;Client&amp;gt; getClientById(long id);
    List&amp;lt;Client&amp;gt; getAll();
}

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;

@Service
public class ClientServiceImpl implements ClientService {
    @Autowired
    private ClientRepository repo;

    @Override
    public void addClient(Client client) {
        repo.save(client);
    }

    @Override
    public Optional&amp;lt;Client&amp;gt; getClientById(long id) {
        return repo.findById(id);
    }

    @Override
    public List&amp;lt;Client&amp;gt; getAll() {
        return repo.findAll();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Description: ClientService and ClientServiceImpl define and implement business logic, decoupling it from the controller layer.&lt;/p&gt;
&lt;h4&gt;
  
  
  Step 4: Creating the Serializer and Deserializer
&lt;/h4&gt;

&lt;p&gt;Convert Client instances to and from SOAP ClientInfo instances.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;```import com.example.client_mgmt.model.Client;&lt;br&gt;
import com.example.client_mgmt.soap.ClientInfo;&lt;br&gt;
import org.springframework.beans.BeanUtils;&lt;/p&gt;

&lt;p&gt;import java.util.ArrayList;&lt;br&gt;
import java.util.List;&lt;/p&gt;

&lt;p&gt;public class ClientSerializer {&lt;br&gt;
    public ClientInfo serialize(Client client) {&lt;br&gt;
        ClientInfo clientInfo = new ClientInfo();&lt;br&gt;
        BeanUtils.copyProperties(client, clientInfo);&lt;br&gt;
        return clientInfo;&lt;br&gt;
    }&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public Client deserialize(ClientInfo clientInfo) {
    Client client = new Client();
    BeanUtils.copyProperties(clientInfo, client);
    return client;
}

public List&amp;lt;ClientInfo&amp;gt; serializeAll(List&amp;lt;Client&amp;gt; clients) {
    List&amp;lt;ClientInfo&amp;gt; clientInfos = new ArrayList&amp;lt;&amp;gt;();
    for (Client client : clients) {
        clientInfos.add(serialize(client));
    }
    return clientInfos;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Description: The ClientSerializer class handles the conversion between your Client entity and the SOAP ClientInfo representation.
#### Step 5: Creating the Endpoint
Define an endpoint to handle SOAP requests, mapping them to your service layer.


```import com.example.client_mgmt.service.ClientService;
import com.example.client_mgmt.soap.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

@Endpoint
public class ClientEndpoint {
    private static final String NAMESPACE_URI = "http://example.com/client_mgmt";

    @Autowired
    private ClientService service;
    @Autowired
    private ClientSerializer serializer;

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getClientsRequest")
    @ResponsePayload
    public GetClientsResponse getClients(@RequestPayload GetClientsRequest request) {
        GetClientsResponse response = new GetClientsResponse();
        List&amp;lt;Client&amp;gt; clients = service.getAll();
        List&amp;lt;ClientInfo&amp;gt; clientInfos = serializer.serializeAll(clients);
        response.getClients().addAll(clientInfos);
        return response;
    }

    // Additional methods for other operations (e.g., addClient)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Description: ClientEndpoint processes incoming SOAP requests and returns appropriate responses by interacting with the service layer.&lt;/p&gt;
&lt;h4&gt;
  
  
  Step 6: Configuring the Web Service
&lt;/h4&gt;

&lt;p&gt;Create a configuration class to set up the SOAP web service.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;```import org.springframework.boot.web.servlet.ServletRegistrationBean;&lt;br&gt;
import org.springframework.context.ApplicationContext;&lt;br&gt;
import org.springframework.context.annotation.Bean;&lt;br&gt;
import org.springframework.context.annotation.Configuration;&lt;br&gt;
import org.springframework.ws.config.annotation.EnableWs;&lt;br&gt;
import org.springframework.ws.config.annotation.WsConfigurerAdapter;&lt;br&gt;
import org.springframework.ws.transport.http.MessageDispatcherServlet;&lt;br&gt;
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;&lt;br&gt;
import org.springframework.xml.xsd.SimpleXsdSchema;&lt;br&gt;
import org.springframework.xml.xsd.XsdSchema;&lt;/p&gt;

&lt;p&gt;@EnableWs&lt;br&gt;
@Configuration&lt;br&gt;
public class WebServiceConfig extends WsConfigurerAdapter {&lt;br&gt;
    &lt;a class="mentioned-user" href="https://dev.to/bean"&gt;@bean&lt;/a&gt;&lt;br&gt;
    public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {&lt;br&gt;
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();&lt;br&gt;
        servlet.setApplicationContext(applicationContext);&lt;br&gt;
        servlet.setTransformWsdlLocations(true);&lt;br&gt;
        return new ServletRegistrationBean(servlet, "/ws/*");&lt;br&gt;
    }&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Bean(name = "clients")
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema clientsSchema) {
    DefaultWsdl11Definition definition = new DefaultWsdl11Definition();
    definition.setPortTypeName("ClientsPort");
    definition.setLocationUri("/ws");
    definition.setTargetNamespace("http://example.com/client_mgmt");
    definition.setSchema(clientsSchema);
    return definition;
}

@Bean
public XsdSchema clientsSchema() {
    return new SimpleXsdSchema(new ClassPathResource("clients.xsd"));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Description: WebServiceConfig sets up the SOAP web service, registering the necessary servlet and defining the WSDL.
#### Step 7: Testing with SOAP-UI
Use SOAP-UI to create test requests and responses. This helps in verifying the correctness of your SOAP services.
Description: SOAP-UI is a powerful tool for testing your SOAP services, ensuring they behave as expected.
#### Final Thoughts
Working with SOAP services in Spring Boot involves a series of well-defined steps, from setting up the project to defining the model, repository, service, and endpoint layers. By leveraging Spring’s powerful features like IoC and DI, along with tools like SOAP-UI for testing, you can build robust SOAP web services efficiently.
#### References
- Dependencies: External libraries or modules your Spring application relies on to function. Examples include Spring Data JPA, Spring Security, and Log4j.
- Plugins: Specialized tools that extend your build process, such as the Maven Compiler Plugin and the Spring Boot Maven Plugin.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
    </item>
  </channel>
</rss>
