第一次自学写webservice给我感觉真的很痛苦,曾经看见老师们几分钟就搞定,可现在我花了几个小时都没弄出处,还出了不少的错,现在我就把这些错记一记,以防以后写webservice又出错找不到解决办法
1. 错误:
Exception in thread "main" javax.xml.ws.WebServiceException: Unable to create
JAXBContext due to the security restriction
(WebServiceException:无法创建JAXBContext由于安全限制)
说明:数据库的数据已经全部读取完了,却还报空指针异常,去修改 Endpoint.publish的实例
Hibernate:
select
student0_.sid as sid1_0_,
student0_.sNAME as sNAME2_0_,
student0_.classId as classId3_0_
from
Students student0_
Student [sid=10, sname=倪明, classId=2]
Student [sid=11, sname=倪明, classId=2]
Student [sid=2, sname=李红, classId=2]
Student [sid=3, sname=黎明, classId=3]
Student [sid=4, sname=张洋, classId=4]
Exception in thread "main" java.lang.NullPointerException
at com.sun.xml.internal.ws.server.EndpointFactory.getWsdlLocation(EndpointFactory.java:385)
at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.getPrimaryWsdl(EndpointImpl.java:265)
at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.createEndpoint(EndpointImpl.java:208)
at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.publish(EndpointImpl.java:138)
at com.sun.xml.internal.ws.spi.ProviderImpl.createAndPublishEndpoint(ProviderImpl.java:92)
at javax.xml.ws.Endpoint.publish(Endpoint.java:170)
at com.youmeng.ws.servlet.StudentWs.main(StudentWs.java:43)
原代码:
@WebService
@SOAPBinding(style=Style.RPC)
public class StudentWs {
StudentDaoImpl daoImpl = new StudentDaoImpl();
public List<Student> selectStudent(){
return (ArrayList<Student>) daoImpl.selectStudent();
}
public static void main(String[] arg){
//发布webService
Endpoint.publish("http://192.168.0.132/student", new StudentWs());
System.out.println("已经连接上IP为:http://192.168.0.132的地址");
}
解决办法:
将List<Student> 改为 ArrayList<Student>
2. 错误
org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://webservice.qxqpw.com/}HelloWorldService from class com.qxqpw.webservice.HelloWorld
Exception in thread "main" javax.xml.ws.WebServiceException: Could not find wsdl:binding operation info for web method sayHi.
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:113)
at $Proxy14.sayHi(Unknown Source)
at com.qxqp.test.TestWebservice.main(TestWebservice.java:12)
解决:
spring cxf 异常:
有两个原因:
客户端的接口的包名(或者接口名)和服务端的包名(或者接口名)不一致
客户端的接口没有加上@Webservice注解
3. 错误
Exception in thread "main" javax.xml.ws.WebServiceException: Could not find wsdl:binding operation info for web method getListPnameByuid.
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:113)
at $Proxy17.getListPnameByuid(Unknown Source)
at cn.navidog.front.users.testwebservice.main(testwebservice.java:21)
解决方法
原因是因为客户端的service中的方法没有跟服务端绑定,只需要将客户端的Service接口加上注解@WebService,异常便解决了。
3.转化为xml形式
引入javax.ws.rs(jaxwr)见下方新文件
在方法上面添加
在方法上加 @GET
@Path("/greeting")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
代码如下
@GET
@Path("/greeting")
@Produces("application/json")
@Connsumes(MediaType.Appliaction_JSON)
public List<Student> getStudentAll(@QueryParam("start")String start,@QueryParam("count") String count));