WSDL is missing elements using Java

27 views Asked by At

I work with Eclipse JAVA-JDK11. I created a request that has 2 elements "customerId" and "newMobileNumber", and the response has 3 elements "newMobileNumber", "status", and "status". The issue now I cannot see any of these elements in the WSDL.

WSDL file:

<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.service.customermanagement.example.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://impl.service.customermanagement.example.com/" name="CustomerServiceImplService">
    <import namespace="http://service.customermanagement.example.com/" location="http://localhost:8080/ws/CustomerService?wsdl=1"/>
    <binding xmlns:ns1="http://service.customermanagement.example.com/" name="CustomerServiceImplPortBinding" type="ns1:CustomerService">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <operation name="updateCustomerMobile">
            <soap:operation soapAction="UpdateCustomerMobile"/>
            <input>
                <soap:body use="literal"/>
            </input>
            <output>
                <soap:body use="literal"/>
            </output>
        </operation>
    </binding>
    <service name="CustomerServiceImplService">
        <port name="CustomerServiceImplPort" binding="tns:CustomerServiceImplPortBinding">
            <soap:address location="http://localhost:8080/ws/CustomerService"/>
        </port>
    </service>
</definitions>

WebServicePublisher.java

package com.example.customermanagement.config;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.List;

import jakarta.xml.ws.Endpoint; 
import com.example.customermanagement.service.impl.CustomerServiceImpl; 
import com.example.customermanagement.util.LoggerUtil;
public class WebServicePublisher { 
      
      
      public static void main(String[] args) {
try { 
    if (isPortInUse(8080)) {
        // If port 8080 is in use, stop the process using it
        killProcessUsingPort(8080, "127.0.0.1");
    }
      System.out.println("Service publishing....");
      LoggerUtil.getLogger().log("Service publishing...");
Endpoint.publish("http://localhost:8080/ws/CustomerService", new
CustomerServiceImpl()); 
System.out.println("Service published!");
LoggerUtil.getLogger().log("Service published!");




} catch (Exception e) {
      System.out.println("failed to publish the service exception: " +e.getMessage());
LoggerUtil.getLogger().log("failed to publish the service exception: " +e.getMessage());
}

} 
      // Method to check if a port is already in use
        private static boolean isPortInUse(int port) {
            try {
                // Attempt to create a server socket on the specified port
                ServerSocket serverSocket = new ServerSocket(port, 0, InetAddress.getByAddress(new byte[] {127,0,0,1}));
                // If successful, close the server socket and return false (port is not in use)
                serverSocket.close();
                return false;
            } catch (IOException e) {
              System.out.println("port in use, exception: " +e.getMessage());
            LoggerUtil.getLogger().log("port in use, exception: " +e.getMessage());
                // If an exception is thrown, it means the port is already in use
                return true;
            }
        }
        
     // Method to stop the process using a specific port
        public static void killProcessUsingPort(int port, String ipAddress) {
            try {
                // Find the PID of the process using the specified port and IP address
                List<String> command = new ArrayList<>();
                command.add("cmd");
                command.add("/c");
                command.add("netstat -ano | findstr :" + port);
                ProcessBuilder processBuilder = new ProcessBuilder(command);
                Process process = processBuilder.start();
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

                String line;
                while ((line = reader.readLine()) != null) {
                    if (line.contains(ipAddress)) {
                        String[] parts = line.trim().split("\\s+");
                        String pid = parts[parts.length - 1];
                        System.out.println("Process using port " + port + " and IP " + ipAddress + " found. PID: " + pid);

                        // Kill the process using its PID
                        killProcessByPID(pid);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public static void killProcessByPID(String pid) {
            try {
                // Kill the process using its PID
                ProcessBuilder processBuilder = new ProcessBuilder("taskkill", "/F", "/PID", pid);
                processBuilder.start();
                System.out.println("Process with PID " + pid + " killed successfully.");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

}

CustomerService.java

package com.example.customermanagement.service;

import jakarta.jws.WebMethod;
import jakarta.jws.WebParam;
import jakarta.jws.WebService;
import jakarta.xml.bind.annotation.XmlElement;

import com.example.customermanagement.dto.UpdateMobileResponse;

@WebService(serviceName = "CustomerService")
public interface CustomerService {
    @WebMethod(action = "UpdateCustomerMobile")
    UpdateMobileResponse updateCustomerMobile(
            @WebParam(name = "customerId") int customerId,
            @WebParam(name = "newMobileNumber") String newMobileNumber
);

}

CustomerServiceImpl.java

package com.example.customermanagement.service.impl;

import jakarta.jws.WebService;

import com.example.customermanagement.dto.UpdateMobileResponse;
import com.example.customermanagement.service.CustomerService;
import com.example.customermanagement.util.LoggerUtil;

@WebService(endpointInterface = "com.example.customermanagement.service.CustomerService")
public class CustomerServiceImpl implements CustomerService {

    @Override
    public UpdateMobileResponse updateCustomerMobile(int customerId, String newMobileNumber) {
        // Implement the logic to update the mobile number.
        // This example always returns success.
        LoggerUtil.getLogger().log("Start UpdateMobileResponse updateCustomerMobile");
        String message = "";
        int status = 0;
        if (newMobileNumber.matches("\\d+")) {
        
         message = "Your mobile update with the new number " + newMobileNumber;
         LoggerUtil.getLogger().log("message = " + message);
        }else {
            status= 1;
            message = "Failed to update your mobile number";
            LoggerUtil.getLogger().log("message = " + message);
        }
        LoggerUtil.getLogger().log("End UpdateMobileResponse updateCustomerMobile");

        return new UpdateMobileResponse(0, newMobileNumber,message); // Assuming 0 is success
    }
}

UpdateMobileResponse.java

package com.example.customermanagement.dto;

import com.example.customermanagement.util.LoggerUtil;

public class UpdateMobileResponse {
    
    private int status;
    private String newMobileNumber;
    private String message;

    // Constructor
    public UpdateMobileResponse(int status, String newMobileNumber, String message) {
        this.status = status;
        this.newMobileNumber = newMobileNumber;
        this.message = message;
        LoggerUtil.getLogger().log("UpdateMobileResponse constructor");
    }

    // Getters and Setters
    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getNewMobileNumber() {
        return newMobileNumber;
    }

    public void setNewMobileNumber(String newMobileNumber) {
        this.newMobileNumber = newMobileNumber;
    }

    public void setMessage(String message) {
        this.message = message;
    }
    
    public String getMessage() {
        return message;
    }

    
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.example.customermanagement</groupId>
    <artifactId>CustomerService</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>CustomerService Maven Webapp</name>
    
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <!-- Define the version of Jakarta XML Web Services API -->
        <jaxws-api.version>4.0.1</jaxws-api.version>
        <!-- Define the version of Jakarta XML SOAP API -->
        <jaxws-soap-api.version>3.0.1</jaxws-soap-api.version>
    </properties>
    
    <dependencies>
    <dependency>
    <groupId>jakarta.platform</groupId>
    <artifactId>jakarta.jakartaee-api</artifactId>
    <version>9.1.0</version>
    <scope>provided</scope>
</dependency>
        <!-- JAX-WS API -->
        <dependency>
            <groupId>jakarta.xml.ws</groupId>
            <artifactId>jakarta.xml.ws-api</artifactId>
            <version>${jaxws-api.version}</version>
        </dependency>
        <!-- Jakarta XML SOAP API -->
        <dependency>
            <groupId>jakarta.xml.soap</groupId>
            <artifactId>jakarta.xml.soap-api</artifactId>
            <version>${jaxws-soap-api.version}</version>
        </dependency>
        <dependency>
    <groupId>com.sun.xml.ws</groupId>
    <artifactId>jaxws-rt</artifactId>
    <version>4.0.2</version>
</dependency>
        <!-- You may need to add other dependencies here -->
    </dependencies>
    
    <repositories>
        <!-- Define your local repository if necessary -->
        <repository>
            <id>local-repo</id>
            <url>file:///C:/jaxws-ri-4.0.2/jaxws-ri/lib</url>
        </repository>
    </repositories>
    
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <!-- Define Maven War Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.0</version> <!-- Make sure to use a recent version -->
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

I tried @WebParam and @Element in the interface and same result. The element will not reflect in the WSDL.

0

There are 0 answers