2016-07-10 154 views
0

我在过去两天努力使用HttpInvokerServiceExporter公开服务。错误说Did not receive successful HTTP response完整的堆栈跟踪详情如下。代码在Git仓库中检查。链接 - Github link使用HttpInvokerServiceExporter给出500错误代码

org.springframework.remoting.RemoteAccessException: Could not access HTTP invoker remote service at [http://localhost:8080/spring-remoting/AccountService]; nested exception is java.io.IOException: Did not receive successful HTTP response: status code = 500, status message = [Internal Server Error] 
org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.convertHttpInvokerAccessException(HttpInvokerClientInterceptor.java:216) 
org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.invoke(HttpInvokerClientInterceptor.java:147) 
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) 
com.sun.proxy.$Proxy8.add(Unknown Source) 
com.remoting.ClientInvoker.getCube(ClientInvoker.java:9) 
org.apache.jsp.process_jsp._jspService(process_jsp.java:118) 
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438) 
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396) 
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 

java.io.IOException: Did not receive successful HTTP response: status code = 500, status message = [Internal Server Error] 
org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor.validateResponse(SimpleHttpInvokerRequestExecutor.java:185) 
org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor.doExecuteRequest(SimpleHttpInvokerRequestExecutor.java:92) 
org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor.executeRequest(AbstractHttpInvokerRequestExecutor.java:138) 
org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.executeRequest(HttpInvokerClientInterceptor.java:194) 
org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.executeRequest(HttpInvokerClientInterceptor.java:176) 
org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.invoke(HttpInvokerClientInterceptor.java:144) 
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) 
com.sun.proxy.$Proxy8.add(Unknown Source) 
com.remoting.ClientInvoker.getCube(ClientInvoker.java:9) 
org.apache.jsp.process_jsp._jspService(process_jsp.java:118) 
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438) 
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396) 
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 

我已经正确遵循相关文件,并执行所有步骤。下面是服务接口:

package com.remoting; 

public interface AccountService { 
    public int add(int a, int b); 
} 

然后服务实现

package com.remoting; 
public class AccountServiceImpl implements AccountService { 
    @Override 
    public int add(int a, int b) { 
     return a + b; 
    } 
} 

然后在web.xml

<?xml version="1.0" encoding="UTF-8"?> 
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
version="3.1"> 
    <display-name>spring-remoting</display-name> 

<!-- The front controller of this Spring Web application, responsible for 
    handling all application requests --> 
    <servlet> 
     <servlet-name>accountExporter</servlet-name> 
     <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>accountExporter</servlet-name> 
     <url-pattern>/AccountService</url-pattern> 
    </servlet-mapping> 
</web-app> 

然后应用程序豆。该应用程序豆放在applicationContext.xml,是/WEB-INF/classes目录内:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> 

<bean id="accountService" class="com.remoting.AccountServiceImpl"></bean> 

<bean name="accountExporter" 
    class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"> 
    <property name="service" ref="accountService" /> 
    <property name="serviceInterface" value="com.remoting.AccountService" /> 
</bean> 
<bean id="httpServer" 
    class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> 
    <property name="serviceUrl" value="http://localhost:8080/spring-remoting/AccountService"></property> 
    <property name="serviceInterface" value="com.remoting.AccountService"></property> 
    </bean> 

</beans> 

我们调用服务我有两个JSP文件和一个辅助类加载豆类和调用该函数:

指数。 JSP

<%@ page language="java" contentType="text/html; charset=UTF-8" 
pageEncoding="UTF-8"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>Insert title here</title> 
    </head> 
    <body> 
    <form action="process.jsp"> 
    Enter Number:<input type="text" name="number" /> <input type="submit" 
     value="cube" /> 
    </form> 
    </body> 
</html> 

process.jsp

<jsp:include page="index.jsp"></jsp:include> 
<hr/> 
<%@page import="com.remoting.ClientInvoker"%> 

<% 
    int number=Integer.parseInt(request.getParameter("number")); 
    out.print("sum of "+number+" is: "+ClientInvoker.sum(number)); 
%> 

最后ClientInvoker

package com.remoting; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class ClientInvoker { 
    public static int sum(int number){ 
     ApplicationContext context = new  ClassPathXmlApplicationContext("classpath:applicationContext.xml"); 
     AccountService service = (AccountService)context.getBean("httpServer"); 
     return service.add(4, 4); 
    } 
} 

applicationContext.xml文件放置在目录/WEB-INF/classes内。

请让我知道我要去哪里错了。

+0

我真的希望你不会在实际应用中使用该代码。您不应该将Java代码放在JSP旁边,因为您不需要重新创建应用程序上下文,因为您需要一个bean(基本上,每次执行该操作时都会重新创建整个应用程序,所以除非您想要运行内存问题,交易问题,奇怪的调试问题的方式)。您调用的URL与“DispatcherServlet”的映射不符。 –

回答

0

我知道这是一个老问题,但今天我偶然发现了HttpInvokerClientInterceptor中发生的同样的错误。就我而言,原因是服务器和客户端使用的两个不同的Java对象版本。由于Spring's HTTP invoker使用Java对象(de)序列化,请确保服务器和客户端使用相同版本的对象/类。

参照this question,它也可以意味着只使用相同的serialVersionUID和不同的类。