Of course! Let's dive into Java ASMX Web Services. This is a foundational technology, and understanding it is key to appreciating the evolution towards more modern approaches like REST and SOAP-based frameworks (like JAX-WS and Spring-WS).

What is an ASMX Web Service?
ASMX stands for ASP.NET Web Services. This is a crucial point: ASMX is a Microsoft .NET technology, not a native Java technology.
However, the question "Java webservice asmx" is very common. It usually means one of two things:
- A developer with a Java background needs to consume (use) an existing .NET ASMX web service. This is the most frequent scenario.
- A developer is looking for the Java equivalent of the simplicity of ASMX for creating SOAP web services.
Let's address both scenarios.
Scenario 1: Consuming a .NET ASMX Web Service in Java
This is a very common task. You have a SOAP endpoint provided by a .NET team, and you need to call it from your Java application. The primary tool for this in the Java world is Apache CXF.

How ASMX Works Under the Hood
When a .NET developer creates an ASMX service, they typically add an [WebService] attribute to a class. By default, this service exposes its methods through a standard SOAP 1.1 endpoint and, more importantly, a WSDL (Web Services Description Language) file. This WSDL is the key to interoperability.
The WSDL file is a machine-readable contract that describes:
- The available methods (operations).
- The data types (parameters and return values) in XML Schema (XSD).
- The communication protocol (SOAP, HTTP, etc.).
Your Java client can read this WSDL and generate all the necessary Java classes (a "client proxy") to make the call as if it were a local method call.
Step-by-Step Guide: Consuming an ASMX Service with Apache CXF
Let's say we want to call a simple ASMX service that looks like this (in C#):

// A sample .NET ASMX service
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class MyService : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld(string name)
{
return "Hello, " + name;
}
}
This service would be available at a URL like http://example.com/MyService.asmx and its WSDL would be at http://example.com/MyService.asmx?wsdl.
Step 1: Set up your Java Project (using Maven)
Add the CXF dependencies to your pom.xml. You need the core library and the WSDL generation tools.
<dependencies>
<!-- CXF Core -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.5.5</version> <!-- Use the latest version -->
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.5.5</version>
</dependency>
<!-- For generating client code from WSDL -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-tools-wsdlto-databinding-jaxb</artifactId>
<version>3.5.5</version>
</dependency>
</dependencies>
Step 2: Generate Java Client Code from the WSDL
CXF comes with a command-line tool called wsdl2java. You can run this from your project's root directory.
# -p: The target package for the generated code
# -d: The directory where the code will be generated (e.g., src/main/java)
# -client: Generates a main method for testing the client
# wsdlLocation: The URL to the WSDL file
mvn org.apache.cxf:cxf-codegen-plugin:wsdl2java \
-DwsdlLocation=http://example.com/MyService.asmx?wsdl \
-Dwsdl=http://example.com/MyService.asmx?wsdl \
-DpackageName=com.example.client \
-DgenerateAll
This command will generate a set of Java files inside src/main/java/com/example/client, including:
MyService.java: The service interface.MyServiceService.java: A factory class to get a proxy instance of the service.MyServiceSoap.java: The interface for the SOAP port (where yourHelloWorldmethod will be).ObjectFactory.java,HelloWorld.java, etc.: JAXB data types for the request and response.
Step 3: Write the Java Client Code
Now, you can use the generated classes to call the web service.
package com.example.client;
public class AsmxClient {
public static void main(String[] args) {
// Create an instance of the service factory
MyServiceService service = new MyServiceService();
// Get the SOAP port (the interface for the web methods)
MyServiceSoap port = service.getMyServiceSoap(); // Or getMyServiceSoap12() for SOAP 1.2
// Call the web method as if it were a local method
String response = port.helloWorld("World from Java");
// Print the result
System.out.println("Response from ASMX service: " + response);
}
}
When you run this, CXF handles all the complexity: creating the SOAP request XML, sending it over HTTP, receiving the SOAP response, and unmarshalling it back into a Java String.
Scenario 2: The Java Equivalent of Creating an ASMX Service
ASMX is loved for its simplicity. You just decorate a C# class and methods, and the .NET framework handles the rest. In Java, the modern, standard equivalent for creating SOAP web services is JAX-WS (Java API for XML Web Services). It's part of Java standard edition (since Java 6) and is just as simple, once you have the right tools.
JAX-WS: The Java "ASMX" for Creating SOAP Services
JAX-WS allows you to annotate a POJO (Plain Old Java Object) to turn it into a web service.
Step 1: Create the Service Endpoint Interface (SEI)
This is the Java interface that defines the web service contract. It's similar to the C# interface.
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.WebParam;
// @WebService marks this class as a web service endpoint
@WebService
public interface GreetingService {
// @WebMethod marks this method as a web service operation
@WebMethod
String getGreeting(@WebParam(name = "name") String name);
}
Step 2: Create the Service Implementation
This is the concrete class that implements the interface.
public class GreetingServiceImpl implements GreetingService {
@Override
public String getGreeting(String name) {
if (name == null || name.trim().isEmpty()) {
return "Hello, stranger!";
}
return "Hello, " + name + "!";
}
}
Step 3: Publish the Service
You need a web server (like Tomcat) or a lightweight embedded server to host the service. For simplicity, let's use Apache CXF again, as it makes publishing incredibly easy.
Using Maven and CXF:
Add the CXF Servlet dependency to your pom.xml.
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.5.5</version>
</dependency>
Create a web.xml file in src/main/webapp/WEB-INF/:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/* 