声明:第一次开发WebService,第一次使用XFire,WebService方面的新手
目的:分享经验、备忘
时间:2008-01-22
错误说明:在调用WebService的时候报错:
- org.codehaus.xfire.fault.XFireFault: Unexpected character '>' (code 62) expected '='
org.codehaus.xfire.fault.XFireFault: Unexpected character '>' (code 62) expected '='
解决方案:在webService应用的web.xml中加入:
- <filter>
- <filter-name>CompressingFilter</filter-name>
- <filter-class>
- com.planetj.servlet.filter.compression.CompressingFilter
- </filter-class>
- <init-param>
- <param-name>debug</param-name>
- <param-value>false</param-value>
- </init-param>
- <init-param>
- <param-name>statsEnabled</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>CompressingFilter</filter-name>
- <url-pattern>/services/*</url-pattern>
- </filter-mapping>
<filter>
<filter-name>CompressingFilter</filter-name>
<filter-class>
com.planetj.servlet.filter.compression.CompressingFilter
</filter-class>
<init-param>
<param-name>debug</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>statsEnabled</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CompressingFilter</filter-name>
<url-pattern>/services/*</url-pattern>
</filter-mapping>
详细说明:
开发环境:
服务器:Tomcat5.0
JDK: JDK1.4.2
IDE: MyEclicpse6.0.0 GA+Eclipse3.3
其他: XFire1.2.6
开发简要说明(代码就是SayHello,这里不再说明):
1、通过MyEclipse建立Web Service Project
2、通过MyEclipse新增WebService:
service name:SayAny
Service Interface: ISayAny
Service Impl. class: ISayAnyImpl
生成的Web.xml如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <servlet>
- <servlet-name>XFireServlet</servlet-name>
- <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
- <load-on-startup>0</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>XFireServlet</servlet-name>
- <url-pattern>/services/*</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
services.xml内容如下:(新手说明:在项目下与WebRoot同级目录下,myEclipse自动生成一个WebServices目录,services.xml在此目录中)
- <beans xmlns="http://xfire.codehaus.org/config/1.0">
- <service>
- <name>SayAny</name>
- <serviceClass>com.jiang.service.ISayAny</serviceClass>
- <implementationClass>com.jiang.service.SayAnyImpl</implementationClass>
- <style>wrapped</style>
- <use>literal</use>
- <scope>application</scope>
- </service>
- </beans>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>SayAny</name>
<serviceClass>com.jiang.service.ISayAny</serviceClass>
<implementationClass>com.jiang.service.SayAnyImpl</implementationClass>
<style>wrapped</style>
<use>literal</use>
<scope>application</scope>
</service>
</beans>
3、直接通过MyEclipse发布到Tomcat上;
4、访问http://localhost/studyWebService/services/SayAny?wsdl可以成功看到一个XML内容,里面内容不在此说明
5、编写测试程序,main方法如下:
- public static void main(String[] args) {
- String serviceURL = "http://localhost/studyWebService/services/SayAny";
-
- String datas = "";
-
- Service serviceModel = new ObjectServiceFactory().create(
- ISayAny.class, null, "", null);
-
- XFireProxyFactory serviceFactory = new XFireProxyFactory();
- try {
- ISayAny service = (ISayAny) serviceFactory.create(
- serviceModel, serviceURL);
- Client client = ((XFireProxy) Proxy.getInvocationHandler(service)).getClient();
- client.setProperty(CommonsHttpMessageSender.GZIP_ENABLED,
- Boolean.TRUE);
- client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, "0");
- datas = service.sayHello("jiang shan");
- System.out.println(datas);
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
public static void main(String[] args) {
String serviceURL = "http://localhost/studyWebService/services/SayAny";
String datas = "";
//service
Service serviceModel = new ObjectServiceFactory().create(
ISayAny.class, null, "", null);
XFireProxyFactory serviceFactory = new XFireProxyFactory();
try {
ISayAny service = (ISayAny) serviceFactory.create(
serviceModel, serviceURL);
Client client = ((XFireProxy) Proxy.getInvocationHandler(service)).getClient();
client.setProperty(CommonsHttpMessageSender.GZIP_ENABLED,
Boolean.TRUE);
client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, "0");
datas = service.sayHello("jiang shan");
System.out.println(datas);
} catch (Exception e) {
e.printStackTrace();
}
}
WebServer获得相应,但在
- datas = service.sayHello("jiang shan");
datas = service.sayHello("jiang shan");
报错:
错误如下:
-
- com.jiang.service.TestAdStatData - Could not invoke service.. Nested exception is org.codehaus.xfire.fault.XFireFault: Unexpected character '>' (code 62) expected '='
- at [row,col {unknown-source}]: [1,708]
- org.codehaus.xfire.XFireRuntimeException: Could not invoke service.. Nested exception is org.codehaus.xfire.fault.XFireFault: Unexpected character '>' (code 62) expected '='
- at [row,col {unknown-source}]: [1,708]
- org.codehaus.xfire.fault.XFireFault: Unexpected character '>' (code 62) expected '='
- at [row,col {unknown-source}]: [1,708]
- at org.codehaus.xfire.fault.XFireFault.createFault(XFireFault.java:89)
- at org.codehaus.xfire.client.Client.onReceive(Client.java:391)
- at org.codehaus.xfire.transport.http.HttpChannel.sendViaClient(HttpChannel.java:139)
- at org.codehaus.xfire.transport.http.HttpChannel.send(HttpChannel.java:48)
- at org.codehaus.xfire.handler.OutMessageSender.invoke(OutMessageSender.java:26)
- at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)
- at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:75)
- at org.codehaus.xfire.client.Client.invoke(Client.java:335)
- at org.codehaus.xfire.client.XFireProxy.handleRequest(XFireProxy.java:77)
- at org.codehaus.xfire.client.XFireProxy.invoke(XFireProxy.java:57)
- at $Proxy0.getAdDataByUidAndPosId(Unknown Source)
- at com.jiang.service.TestAdStatData.main(TestAdStatData.java:37)
- Caused by: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '>' (code 62) expected '='
- at [row,col {unknown-source}]: [1,708]
- at com.ctc.wstx.sr.StreamScanner.throwUnexpectedChar(StreamScanner.java:623)
- at com.ctc.wstx.sr.BasicStreamReader.handleNsAttrs(BasicStreamReader.java:2999)
- at com.ctc.wstx.sr.BasicStreamReader.handleStartElem(BasicStreamReader.java:2934)
- at com.ctc.wstx.sr.BasicStreamReader.nextFromTree(BasicStreamReader.java:2846)
- at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1019)
- at org.codehaus.xfire.soap.handler.ReadHeadersHandler.invoke(ReadHeadersHandler.java:44)
- at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)
- at org.codehaus.xfire.client.Client.onReceive(Client.java:387)
- ... 10 more
com.jiang.service.TestAdStatData - Could not invoke service.. Nested exception is org.codehaus.xfire.fault.XFireFault: Unexpected character '>' (code 62) expected '='
at [row,col {unknown-source}]: [1,708]
org.codehaus.xfire.XFireRuntimeException: Could not invoke service.. Nested exception is org.codehaus.xfire.fault.XFireFault: Unexpected character '>' (code 62) expected '='
at [row,col {unknown-source}]: [1,708]
org.codehaus.xfire.fault.XFireFault: Unexpected character '>' (code 62) expected '='
at [row,col {unknown-source}]: [1,708]
at org.codehaus.xfire.fault.XFireFault.createFault(XFireFault.java:89)
at org.codehaus.xfire.client.Client.onReceive(Client.java:391)
at org.codehaus.xfire.transport.http.HttpChannel.sendViaClient(HttpChannel.java:139)
at org.codehaus.xfire.transport.http.HttpChannel.send(HttpChannel.java:48)
at org.codehaus.xfire.handler.OutMessageSender.invoke(OutMessageSender.java:26)
at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)
at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:75)
at org.codehaus.xfire.client.Client.invoke(Client.java:335)
at org.codehaus.xfire.client.XFireProxy.handleRequest(XFireProxy.java:77)
at org.codehaus.xfire.client.XFireProxy.invoke(XFireProxy.java:57)
at $Proxy0.getAdDataByUidAndPosId(Unknown Source)
at com.jiang.service.TestAdStatData.main(TestAdStatData.java:37)
Caused by: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '>' (code 62) expected '='
at [row,col {unknown-source}]: [1,708]
at com.ctc.wstx.sr.StreamScanner.throwUnexpectedChar(StreamScanner.java:623)
at com.ctc.wstx.sr.BasicStreamReader.handleNsAttrs(BasicStreamReader.java:2999)
at com.ctc.wstx.sr.BasicStreamReader.handleStartElem(BasicStreamReader.java:2934)
at com.ctc.wstx.sr.BasicStreamReader.nextFromTree(BasicStreamReader.java:2846)
at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1019)
at org.codehaus.xfire.soap.handler.ReadHeadersHandler.invoke(ReadHeadersHandler.java:44)
at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)
at org.codehaus.xfire.client.Client.onReceive(Client.java:387)
... 10 more
解决方案本文上面所说:加入pjl-comp-filter-1.4.6.jar,并在Web.xml中配置filter即可。
出错原因:这句有问题:
- client.setProperty(CommonsHttpMessageSender.GZIP_ENABLED,Boolean.TRUE);
client.setProperty(CommonsHttpMessageSender.GZIP_ENABLED,Boolean.TRUE);
客户端调用时设置了GZIP参数,但Server端没有用GZIP过滤文件!