WebService学习笔记0001

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这个方法返回的字符串了



猜你喜欢

转载自lanccj.iteye.com/blog/1463329