You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中调用WebService指定@WebMethod的方法及参数传递问询

How to Call JAX-WS WebMethods from Browser & Pass Parameters

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

  1. Open your browser's developer tools (F12) → Go to the "Network" tab.
  2. Create a new POST request to http://localhost:8000/rs.
  3. Set the Content-Type header to text/xml; charset=utf-8.
  4. Paste this SOAP payload for the GetNextOne method:
<?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>
  1. 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:

  1. Open soapUI, create a new SOAP project, and enter your WSDL URL: http://localhost:8000/rs?wsdl.
  2. Expand the project → Find the GetNext operation.
  3. 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>
  1. 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:

  1. Run the wsimport tool (included with JDK) to generate client classes:
wsimport -keep http://localhost:8000/rs?wsdl
  1. 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);
        }
    }
}
  1. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:12:13