WebService学习笔记0001

:arrow: WebService学习笔记0001

前提条件:
1.JDK1.6.0_22以上版本

遇到报错:
1.

严重: Request doesnt have a Content-Type
com.sun.xml.internal.ws.server.UnsupportedMediaException: Request doesnt have a Content-Type


解决方法:问题出在在访问地址的时候少打了?wsdl
2.

Exception in thread "main" com.sun.xml.internal.ws.model.RuntimeModelerException: runtime modeler error: Wrapper class com.lanccj.service.jaxws.Hello is not found. Have you run APT to generate them?

解决方法:在服务接口注解再添加如下注解

@SOAPBinding(style = SOAPBinding.Style.RPC)


代码压缩包:见附件

全部代码粘贴:
服务接口类,IWebservice .java

package com.lanccj.service;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface IWebservice {
public String hello();
}




服务实现类,Webservice.java

package com.lanccj.service;

import javax.jws.WebService;

@WebService(endpointInterface="com.lanccj.service.IWebservice")
public class Webservice implements IWebservice {
@Override
public String hello() {
return "你好!WebService";
}
}


服务发布测试类,TestService.java

package com.lanccj.service;

import javax.xml.ws.Endpoint;

public class TestService {

public static void main(String[] args) {
String url="http://localhost:9999/service";
Endpoint.publish(url, new Webservice());
}
}


通过以上的代码书写能够发布最简单的WebService,能够让初学者了解入门,通过以上操作我们可以通过地址访问:

http://localhost:9999/service?wsdl看到XML描述文件

我们再写一个类用来测试调用服务的这个放法


package com.lanccj.service;

import java.net.MalformedURLException;
import java.net.URL;

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

public class TestClient {


public static void main(String[] args) {
try {
URL url=new URL("http://localhost:9999/service?wsdl");
QName sname=new QName("http://service.lanccj.com/","WebserviceService");
//这边两个参数是从刚才网址的XML中获取的
//targetNamespace="http://service.lanccj.com/"name="WebserviceService"
//可以找到
Service service=Service.create(url,sname);
IWebservice ms=service.getPort(IWebservice.class);
System.out.println(ms.hello());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}

}



我们可以在控制台看到hello这个方法返回的字符串了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值