java.net.MalformedURLException: Invalid address. Endpoint address cannot be null.

本文介绍了一个简单的 Java Web 服务实现方法,包括服务端接口定义、实现类及发布过程,并展示了客户端如何调用该服务。文章还提供了解决客户端调用过程中遇到的 URL 异常问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface HelloWorld {
String sayHello(@WebParam(name="username")String username);

}


import javax.jws.WebService;

@WebService(serviceName="HelloWorld")
public class HelloWorldImpl implements HelloWorld {

@Override
public String sayHello(String username) {
return "Hello:"+username;
}

}


import javax.xml.ws.Endpoint;

public class Server {

/**
* @param args
*/
public static void main(String[] args) {
HelloWorld hello=new HelloWorldImpl();
String address="http://localhost:8081/ws/HelloWorldService";
Endpoint.publish(address, hello);
System.out.println("Servet start...");
}
}


import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;

public class Client {
private static final QName SERVICE_NAME=new QName(
"http://endpoint.cxf.webservice.test/",//xmlns:tns="http://endpoint.cxf.webservice.test/"
"HelloWorld"//<wsdl:binding name="helloWorldServiceSoapBinding" type="tns:HelloWorldServiceInf">
);
private static final QName PORT_NAME=new QName(
"http://endpoint.cxf.webservice.test/",//xmlns:tns="http://endpoint.cxf.webservice.test/"
"HelloWorldImplPort"//<wsdl:port binding="tns:helloWorldServiceSoapBinding" name="HelloWorldServiceImplPort">
);
public static void main(String[] args) {
        String endPointAddress="http://localhost:8081/ws/HelloWorldService";
        Service service=Service.create(SERVICE_NAME);
        service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endPointAddress);
        HelloWorld inf=service.getPort(HelloWorld.class);
        System.out.println(inf.sayHello("张三"));
    }
}

----------------------------------------------------------------

执行Client报异常:java.net.MalformedURLException: Invalid address. Endpoint address cannot be null.

解决方法:

HelloWorld inf=service.getPort(HelloWorld.class);--------------->HelloWorld inf=service.getPort(PORT_NAME, HelloWorld.class);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值