axis2的方法调用以及302 Error: Moved Temporarily的解决方法

AddBillSuccessCallBack.java是一个普通的action里面的方法就是接受2个参数,并更新数据库某个字段,

把这个类配置到webservice中去。

其它配置就不说了,主要想说的是在调用的时候出现的问题,这是配置

services.xml

<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one
  ~ or more contributor license agreements. See the NOTICE file
  ~ distributed with this work for additional information
  ~ regarding copyright ownership. The ASF licenses this file
  ~ to you under the Apache License, Version 2.0 (the
  ~ "License"); you may not use this file except in compliance
  ~ with the License. You may obtain a copy of the License at
  ~
  ~ http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing,
  ~ software distributed under the License is distributed on an
  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  ~ KIND, either express or implied. See the License for the
  ~ specific language governing permissions and limitations
  ~ under the License.
  -->

<service name="afterAddBillMessage">
    <description>
        This service is to give message to the calling for add a bill
    </description>
    <parameter name="ServiceClass">org.hd.axis2.device.AddBillSuccessCallBack</parameter>
    <operation name="hadAddBillForTheCall">
    <messageReceiver  class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
    </operation>
</service>


package org.hd.axis2.device;

import org.apache.log4j.Logger;
import org.hd.business.service.BusinessService;
import org.hd.business.summary.model.CallSummaryVo;
import org.hd.login.action.LoginAction;
import org.rd.framework.common.container.ContainerManager;

public class AddBillSuccessCallBack{
	private static final Logger logger = Logger.getLogger(AddBillSuccessCallBack.class.getName());
	private BusinessService businessService = (BusinessService)ContainerManager.getComponent(BusinessService.BEAN_ID);
	
	public String hadAddBillForTheCall(String callid,String billNumber){
		String resultMessage="";
		CallSummaryVo call=new CallSummaryVo();
		if(callid==null||callid.trim().equals("")){
			resultMessage="未获取到通话callid,操作失败!";
		}else if(billNumber==null||billNumber.trim().equals("")){
			resultMessage="未获取到工单号billNumber,操作失败!";
		}else{
			call.setVoiceid(callid);
			call.setBill_number(billNumber);
			businessService.updateSummaryByWebservice(call);
			resultMessage="收到信息:callid="+callid+",billNumber="+billNumber;
		}
		System.out.println("调用webservice的结果信息:"+resultMessage);
		logger.error("调用webservice的结果信息:"+resultMessage);
		return resultMessage;
		
	}
}


上面我们配置好了webservice,现在就看看调用的效果,遇到一个错误:

Exception in thread "main" org.apache.axis2.AxisFault: Transport error: 302 Error: Moved Temporarily
at org.apache.axis2.transport.http.HTTPSender.handleResponse(HTTPSender.java:310)
at org.apache.axis2.transport.http.HTTPSender.sendViaPost(HTTPSender.java:194)
at org.apache.axis2.transport.http.HTTPSender.send(HTTPSender.java:75)
at org.apache.axis2.transport.http.CommonsHTTPTransportSender.writeMessageWithCommons(CommonsHTTPTransportSender.java:404)
at org.apache.axis2.transport.http.CommonsHTTPTransportSender.invoke(CommonsHTTPTransportSender.java:231)
at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:443)
at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:406)
at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:229)
at org.apache.axis2.client.OperationClient.execute(OperationClient.java:165)
at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:555)
at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:531)
at org.apache.axis2.rpc.client.RPCServiceClient.invokeBlocking(RPCServiceClient.java:102)
at org.hd.axis2.client.AddBillSuccessCallBackClient.main(AddBillSuccessCallBackClient.java:36)


302这个错简简单单几行也找不到原因,从网上看了看有个官方的解释比较可信:

HTTP错误302 - 临时移动

Introduction 介绍

Your Web server thinks that your URL has been temporarily redirected to another URL.您的Web服务器认为您的网址已经被暂时重定向到另一个URL。 The client system is expected to immediately retry the alternate URL.客户端系统立即重试替代的URL。

302 errors in the HTTP cycle 302中的错误的HTTP周期

Any client (eg your Web browser or our CheckUpDown robot) goes through the following cycle when it communicates with the Web server:任何客户端(如Web浏览器或我们的CheckUpDown机器人)经过下一个周期,当它与Web服务器进行通信:

•Obtain an IP address from the IP name of the site (the site URL without the leading 'http://'). IP的站点名称(网站URL没有起始的'http://')获得一个IP地址。 This lookup (conversion of IP name to IP address) is provided by domain name servers (DNSs).这个对应关系(IP名称到IP地址的转换)提供域名服务器(DNSs)。
•Open an IP socket connection to that IP address.打开一个IP套接字连接到该IP地址。
•Write an HTTP data stream through that socket.通过该套接字写HTTP数据流。
•Receive an HTTP data stream back from the Web server in response.接收从Web服务器响应的HTTP数据流。 This data stream contains status codes whose values are determined by the HTTP protocol.该数据流包括状态编码,其值是由HTTP协议。 Parse this data stream for status codes and other useful information.解析该数据流的状态码和其他有用的信息。
This error occurs in the final step above when the client receives an HTTP status code that it recognises as '302'.这个错误发生在以上时,客户端收到HTTP状态代码,并识别其为'302'的最后一步。

Resolving 302 errors - general 解决302错误-一般

The 302 response from the Web server should always include an alternative URL to which redirection should occur. 302响应从Web服务器应始终包括的替代URL重定向应该发生的。 If it does, a Web browser will immediately retry the alternative URL.如果是的话,Web浏览器会立即重试另一个URL。 So you never actually see a 302 error in a Web browser, unless perhaps you have a corrupt redirection chain eg URL A redirects to URL B which in turn redirects back to URL A. If your client is not a Web browser, it should behave in the same way as a Web browser ie immediately retry the alternative URL.所以,你从来没有真正在Web浏览器中看到一个302错误,除非也许你有一个的重定向链,如一个URL重定向到网址B,这反过来又重定向到URL A.如果您的客户端不是一个Web浏览器,它应该表现在以同样的方式作为一个Web浏览器,即立即重试另一个URL。

If the Web server does not return an alternative URL with the 302 response, then either the Web server sofware itself is defective or the Webmaster has not set up the URL redirection correctly.如果Web服务器没有返回302响应的替代URL,然后的Web服务器sofware本身是有缺陷的或管理员没有设置正确的URL重定向。

Resolving 302 errors - CheckUpDown 解决302错误- CheckUpDown

Redirection of URLs may occur for low-level URLs (specific URLs within the Web site such aswww.isp.com/products/index.html ) when you reorganise the web site, but is relatively uncommon for the top-level URLs (such aswww.isp.com ) which most CheckUpDown users ask us to check.当你重定向的URL,可能会出现低级别的网址(网站内,如www.isp.com /产品/ index.html的特定URL)重组的网站,但相对少见的顶级的URL(例如作为www.isp.com)其中大部分的CheckUpDown用户要求我们检查。 So this error should be fairly infrequent.因此,这个错误应该是相当罕见的。

The 302 response from the Web server should always include an alternative URL to which redirection should occur. 302响应从Web服务器应始终包括的替代URL重定向应该发生的。 If it does, CheckUpDown automatically tries the alternative URL.如果是这样,的CheckUpDown会自动尝试另一个URL。 This in turn may possibly lead to another redirection which CheckUpDown then tries.这反过来又可能导致另一个重定向的CheckUpDown然后尝试。 This continues for a maximum of 5 redirections.这最多持续5重定向。 As soon as 5 redirections have occurred, CheckUpDown gives up and reports the 302 error for your account.只要5重定向时有发生,的CheckUpDown放弃,并报告302错误您的帐户。 So you should only ever see the 302 error if 1) the Web server gives no alternative URL on the 302 response or 2) the number of redirections exceeds 5.所以,你应该只看到302错误:1)Web服务器没有给出替代URL的302响应或2)重定向的数量超过5个。 This second condition should be fairly unlikely - and may indicate a recursive pattern eg URL A redirects to URL B which in turn redirects back to URL A.这第二个条件应该是相当不可能的 - 一个可能表明一个递归模式,例如URL重定向到网址B,这反过来又重定向到URL A.

You first need to check that the IP name we use to check for your account is accurate.您首先需要检查IP名称,我们用它来检查您的帐户是准确的。 If you or your ISP have configured something so that any access using this name should now be redirected to another name, then you need to update your CheckUpDown account to start using the new name.如果您或您的ISP已经配置了一些东西,使任何接入使用这个名字,现在应该被重定向到另外一个名字,那么你需要更新您的CheckUpDown帐户,开始使用新的名称。

If you believe that the IP name we use is exact (should not be redirected), please try accessing the current URL using a Web browser.如果您认为我们使用的IP名称是准确的(不应该被重定向),请尝试使用Web浏览器访问的URL。 Note carefully which URL actually gets displayed, because your browser may silently switch to a substitute URL if it receives an 302 message from the Web server.请注意的URL被显示,因为您的浏览器可能会静静地切换到一个替代URL,如果它接收到一个302消息从Web服务器。 If you see any evidence of a new URL, try accessing that directly from your browser.如果你看到任何证据的一个新的URL,请尝试访问,直接从您的浏览器。 If this works (you see the Web site as expected), then this new URL is what you may need to update on your CheckUpDwon account.如果这样的作品(你看的网站如预期),那么这个新的网址是什么,你可能需要更新在您的CheckUpDwon帐户。 If this is a temporary redirection, then you may also need to reinstate the original IP name at a later date when the redirection is no longer effective.如果这是一个临时的重定向,那么你可能还需要在以后的日子不再有效时,重定向恢复原来的IP名称。

If none of the above help, we can analyse the underlying HTTP data streams we receive from the Web server.如果没有上述的帮助,我们可以分析的基础我们收到来自Web服务器的HTTP数据流。 These can provide additional information about the new URL(s) which the Web server thinks we should be accessing.这些新的URL(S)认为,我们应该访问的Web服务器,可以提供额外的信息。 Before doing this, we prefer you to identify any deliberate changes on your side, liaising with your ISP if needs be.在此之前,我们更,你识别任何故意的变化就在你身边,与你的ISP联系,如果需要的话。

302 errors should occur infrequently, because top-level URLs do not change often. 302错误应该很少发生,因为顶级的URL不经常更改。 If they do change, then this is typically because a redirection URL is being suggested.如果他们不改变,那么这通常是因为重定向URL建议。 This pervasive change is unlikely to occur by accident, so most often we can resolve this error by updating our system records for your account following a deliberate change of URL on your part, whether this change is temporary or permanent.这种无处不在的变化是不太可能发生意外,所以多数时候我们可以解决这个错误,更新我们的系统记录您的帐户后,故意URL上的变化,这种变化是暂时的还是永久的。

也就是说url不对,我以前调用过是可以成功调用的,后来就不对了,让我很疑惑。后来想到是不是被拦截了,

查看了一下工程中的拦截器过滤器,在里面加上了这个地址,让工程不拦截这个地址,最后果然成功了。

web.xml

<filter>
<filter-name>securityFilter</filter-name>
<filter-class>org.rd.framework.common.filter.SecurityFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>securityFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>

org.rd.framework.common.filter.SecurityFilter

package org.rd.framework.common.filter;

import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.rd.framework.util.Constants;

public class SecurityFilter extends AbstractFilter {

	@Override
	protected void doFilterInternal(HttpServletRequest request,
			HttpServletResponse response, FilterChain chain)
			throws ServletException, IOException {

		HttpSession session = request.getSession();
		String userid = (String) session.getAttribute(Constants.CURRENT_USER_ID);
		if (userid != null && !"".equals(userid)) {
//			session.setAttribute(Constants.CURRENT_USER_ID, userid);
			chain.doFilter(request, response);
			return;
		}
		String suffixal = request.getRequestURI().substring(request.getRequestURI().lastIndexOf(".") + 1);
		if ("css".equals(suffixal) || 
		    "gif".equals(suffixal) ||
		    "jpg".equals(suffixal) ||
		    "html".equals(suffixal) ||
		    "htm".equals(suffixal) ||
			"js".equals(suffixal) || 
			"png".equals(suffixal) ||
			//"jsp".equals(suffixal) ||
			"xml".equals(suffixal)) {
			chain.doFilter(request, response);
			return;
		}
		if(request.getRequestURI().contains("afterAddBillMessage")||request.getRequestURI().contains("services")){
			chain.doFilter(request, response);
			System.out.println("webservice请求,经过过滤!");
			return;
		}
		suffixal = request.getRequestURI().substring(request.getRequestURI().lastIndexOf("/") + 1);
		if ("login.action".equals(suffixal)) {
			chain.doFilter(request, response);
			return;
		}
		
		if ("hdlogin.jsp".equals(suffixal)) {
			chain.doFilter(request, response);
			return;
		}
		
		if ("loginOut.action".equals(suffixal)) {
			chain.doFilter(request, response);
			return;
		}
		if ("ajaxLogin.action".equals(suffixal)) {
			chain.doFilter(request, response);
			return;
		}
		
		/*if ("left.action".equals(suffixal)) {
			chain.doFilter(request, response);
			return;
		}
		
		if ("right.action".equals(suffixal)) {
			chain.doFilter(request, response);
			return;
		}*/
		
//		if (null == suffixal || "".equals(suffixal)) {
//			chain.doFilter(request, response);
//			return;
//		}
//		
//		if (request.getRequestURI().indexOf("test") > -1 ) {
//			chain.doFilter(request, response);
//			return;
//	    }
		response.sendRedirect(request.getContextPath() + "/hdlogin.jsp");
	}

}


关于wbservice,我还想说一说方法的调用,一般我们需要使用java代码来调用,

也就是客户端代码,这样的代码有很多种,我想说的不是这个而是在浏览器地址栏的调用。

写好了webservice肯定要调试,如果都在main函数中调用,每次都要run一下,

其实webservice就是xml,我们在浏览器中完全可以直接调用!

比如上面的例子,如果我的项目名叫HelpDesk,端口使用8095

那么在浏览器地址栏输入

http://localhost:8095/HelpDesk/services/afterAddBillMessage?wsdl
http://localhost:8095/HelpDesk/services/afterAddBillMessage?xsd

这样能看到完整的webservice的xml内容,其中wsdl和xsd是不同的解析协议(猜的),

不同的协议达到的效果是一样的。

如果要调用上面类中的方法可以输入:

http://localhost:8095/HelpDesk/services/afterAddBillMessage?hadAddBillForTheCall
返回结果:
- <ns:hadAddBillForTheCallResponse xmlns:ns="
http://device.axis2.hd.org">
<ns:return>未获取到通话callid,操作失败!</ns:return>
</ns:hadAddBillForTheCallResponse>
这个方法需要传参,所以报上面的错误。

改成:

http://localhost:8095/HelpDesk/services/afterAddBillMessage/hadAddBillForTheCall?callid=449758D8FFFFFFF900C44EA936870D14&billNumber=ceshi1

成功!
返回结果:
- <ns:hadAddBillForTheCallResponse xmlns:ns="
http://device.axis2.hd.org">
<ns:return>收到信息:callid=449758D8FFFFFFF900C44EA936870D14,billNumber=ceshi1</ns:return>
</ns:hadAddBillForTheCallResponse>


如果传参错误会报错,我们可以根据这个返回错误信息查找错误原因,比如我传参错误
http://localhost:8095/HelpDesk/services/afterAddBillMessage/hadAddBillForTheCall?callid='449758D8FFFFFFF900C44EA936870D14'&billNumber='ceshi1'

- <soapenv:Reason xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Text xml:lang="en-US">unexpected token: 449758D near line 1, column 88 [select call from org.hd.business.summary.model.CallSummaryVo call where call.voiceid=''449758D8FFFFFFF900C44EA936870D14'']; nested exception is org.hibernate.hql.ast.QuerySyntaxException: unexpected token: 449758D near line 1, column 88 [select call from org.hd.business.summary.model.CallSummaryVo call where call.voiceid=''449758D8FFFFFFF900C44EA936870D14'']</soapenv:Text>
</soapenv:Reason>

根据返回信息原来是sql拼写的错误,看一看传参,就是多加了单引号。

以下是客户端调用代码,可作比较:

package org.hd.axis2.client;

import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

public class AddBillSuccessCallBackClient {
	
	public static void main(String args[]) throws AxisFault {
		// 使用RPC方式调用WebService
		RPCServiceClient serviceClient = new RPCServiceClient();
		Options options = serviceClient.getOptions();
		// 指定调用WebService的URL
		//http://10.23.130.118:8095/HelpDesk/services/afterAddBillMessage
		
		
		//以下测试端口是80,工程名是ROOT(为了配置域名不用写工程名和端口),所以默认不用写;另外在hosts中
		//配置了lsy.helpdesk.citicsinfo.com对应127.0.0.1,所以和localhost等效
		// http://localhost/services/afterAddBillMessage 成功
		// http://127.0.0.1/services/afterAddBillMessage 成功
		// http://127.0.0.1:80/services/afterAddBillMessage 成功
		// http://lsy.helpdesk.citicsinfo.com/services/afterAddBillMessage  成功
		// http://lsy.helpdesk.citicsinfo.com:80/services/afterAddBillMessage  成功
		EndpointReference targetEPR = new EndpointReference(
				"http://lsy.helpdesk.citicsinfo.com:80/services/afterAddBillMessage");
		options.setTo(targetEPR);
		// 指定hadAddBillForTheCall方法的参数值
		Object[] opAddEntryArgs = new Object[] { "449758D8FFFFFFF900C44EA936870D14","22lumberIUERREO9834-SDF" };
		// 指定sayHelloToPerson方法返回值的数据类型的Class对象
		Class[] classes = new Class[] { String.class };
		// 指定要调用的sayHelloToPerson方法及WSDL文件的命名空间
		// QName opAddEntry = new QName("http://ws.apache.org/axis2",
		// "sayHelloMethod");
		// QName opAddEntry = new QName("http://service", "sayHelloMethod");
		QName opAddEntry = new QName("http://device.axis2.hd.org",
				"hadAddBillForTheCall");

		// 调用sayHelloToPerson方法并输出该方法的返回值
		System.out.println(serviceClient.invokeBlocking(opAddEntry,
				opAddEntryArgs, classes)[0]);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值