Java中调用WebService指定@WebMethod的方法及参数传递问询
Hey there, let's work through your two JAX-WS WebService questions step by step:
1. Correct Way to Call WebMethods from Browser
First, your initial attempt to access http://localhost:8000/rs/GetNextOne?wsdl was off the mark—here's what you need to do:
Step 1: Verify Your Service is Published Properly
First, check if your WSDL is accessible by visiting:http://localhost:8000/rs?wsdl
If you see a well-formed XML document, your service is up and running. The ?wsdl suffix goes directly on the endpoint URL, not the method name.
Step 2: Call WebMethods from Browser
JAX-WS uses SOAP by default, which requires a POST request with an XML payload—browsers send GET requests by default, so you can't just type the method URL directly. Here are two ways to make it work:
Option A: Use Browser Dev Tools to Send a SOAP POST
- Open your browser's developer tools (F12) → Go to the "Network" tab.
- Create a new POST request to
http://localhost:8000/rs. - Set the
Content-Typeheader totext/xml; charset=utf-8. - Paste this SOAP payload for the
GetNextOnemethod:
<?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:rand="http://rand/"> <soapenv:Header/> <soapenv:Body> <rand:GetNextOne/> </soapenv:Body> </soapenv:Envelope>
- Send the request—you'll get a SOAP response with the random integer.
Option B: Enable GET Requests for Simple Methods
If you want to call GetNextOne directly via a browser GET request, modify your RandService class to add HTTP method annotations (requires a JAX-WS implementation like Metro):
package rand; import javax.jws.WebService; import javax.jws.WebMethod; import javax.xml.ws.http.HTTPMethod; import java.util.Random; @WebService public class RandService { private static final int maxRands = 16; @WebMethod(operationName = "GetNextOne") @HTTPMethod("GET") // Add this line to enable GET access public int next1() { return new Random().nextInt(); } // ... rest of your existing code }
Restart the publisher, then visit http://localhost:8000/rs/GetNextOne—you'll see the random integer directly in the browser.
2. Calling the GetNext (nextN) Method with Parameters
To call the method that takes an integer parameter and returns an array, you have a few straightforward options:
Option 1: Use a SOAP Testing Tool (e.g., soapUI)
This is the easiest way for testing:
- Open soapUI, create a new SOAP project, and enter your WSDL URL:
http://localhost:8000/rs?wsdl. - Expand the project → Find the
GetNextoperation. - Double-click "Request" and enter your desired parameter value (e.g.,
5) in the placeholder:
<rand:GetNext> <arg0>5</arg0> <!-- Replace with your number; check WSDL for exact parameter name if needed --> </rand:GetNext>
- Send the request—you'll get a SOAP response with the array of random integers.
Option 2: Generate a Java Client with wsimport
If you want to call the service from another Java app:
- Run the
wsimporttool (included with JDK) to generate client classes:
wsimport -keep http://localhost:8000/rs?wsdl
- Use the generated classes to write a client:
public class RandServiceClient { public static void main(String[] args) { // Initialize the service and port RandServiceService service = new RandServiceService(); RandService port = service.getRandServicePort(); // Call GetNext with parameter 5 int[] randomNumbers = port.getNext(5); // Print the result System.out.println("Random numbers:"); for (int num : randomNumbers) { System.out.println(num); } } }
- Compile and run the client—you'll see the array of random integers in the console.
Option 3: Use curl to Send a SOAP POST Request
If you prefer command-line tools:
curl -X POST -H "Content-Type: text/xml; charset=utf-8" -d '<?xml version="1.0"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:rand="http://rand/"> <soapenv:Header/> <soapenv:Body> <rand:GetNext> <arg0>5</arg0> <!-- Replace with your desired number --> </rand:GetNext> </soapenv:Body> </soapenv:Envelope>' http://localhost:8000/rs
You'll get a SOAP response with the array of random integers as XML.
内容的提问来源于stack exchange,提问作者JAN

