gpt4 book ai didi

java - 使用 Apache HttpClient Api 时 Java 对象的反序列化不起作用

转载 作者:行者123 更新时间:2023-12-02 04:38:42 25 4
gpt4 key购买 nike

我是 Spring MVC 的新手。我使用的是 Spring 版本 4.1.6,并在 tomcat 7 上部署了两个 Web 应用程序 A 和 B 作为开发环境。但在实际生产环境中,应用程序A将部署在weblogic上,应用程序B将部署在websphere上。以下是开发环境中发生的场景。

从应用程序 A 中,我提交 jsp 页面并调用 Controller 的以下方法。通过此方法,我创建 HttpPost 请求,并使用 ApacheHttpClient api 将 RequestDetails 域对象发送到另一个 Controller 的方法。这是来自发送方或应用程序 A 的 Controller 的代码。

@RequestMapping(value="/httprequestJinesh.cvr",method = RequestMethod.POST)
public @ResponseBody String createMediaRequest(@RequestBody RequestDetails requestDetails ,HttpServletRequest request, HttpServletResponse response ) throws Exception{
System.out.println("****************** createMediaRequest Method of the controller gets invoked 123 *****************");
if(requestDetails!=null){
System.out.println("******************* Requestdetails Is Been Object is been received ***************" + requestDetails.getRequestId());
}

//Sending the HttpPostRequest
StringBuilder url = new StringBuilder();
String serverUrl = "http://localhost:8080/raws/createMediaRequest.raws"; //get it from app_properpties table
url.append(serverUrl);

System.out.println("************** Started creating the Httpost request ********************");
final HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 10000);

HttpClient httpClient = new DefaultHttpClient(httpParams);
HttpPost httppostRequest = new HttpPost(serverUrl);
httppostRequest.setEntity(new SerializableEntity(requestDetails,false));

System.out.println("************** Finished creating the Http Post request ********************");

System.out.println("*********** Before sending the Httppost Request ******************");

HttpResponse httpResponse = httpClient.execute(httppostRequest);

System.out.println("*********** After sending teh Httppost Request ******************");
return "SUCCESS";
}

在接收器应用程序的 Controller 上,我尝试反序列化 POST 请求中收到的域对象。应用程序 A 和 B 都在工作区中提供了 RequestDetails 类,但在这两种情况下,域对象的包层次结构是不同的。例如,在应用程序 A 中,RequestDetails 对象在 com.test 和应用程序 B 中可用。可在 com.test123 中找到。以下是接收器应用程序 Controller 上的代码。

RequestMapping(value = "/createMediaRequest.raws", method = RequestMethod.POST)
public Object createMediaRequest(HttpServletRequest request, HttpServletResponse response){
System.out.println("***************** MediaWorkflowController Received Media Request *******************");

try{
ObjectInputStream in = new ObjectInputStream(request.getInputStream());
RequestDetails requestDetails=(RequestDetails)in.readObject();
if(requestDetails!=null){
System.out.println("requestdetails object is not null *******************" + requestDetails.getRequestId());
}
}
catch(Exception e){
e.printStackTrace();
}

return null;
}

下面是应用程序 A 上的 RequestDetails.java 的代码

package com.test;
public class RequestDetails implements java.io.Serializable{

String requestId;

public String getRequestId() {
return requestId;
}
public void setRequestId(String requestId) {
this.requestId = requestId;
}
}

下面是应用程序 B 上的 RequestDetails.java 的代码

package com.test123;
public class RequestDetails implements java.io.Serializable{

String requestId;

public String getRequestId() {
return requestId;
}
public void setRequestId(String requestId) {
this.requestId = requestId;
}
}

当执行接收器应用程序 Controller 的代码时,我收到以下异常。

****************** createMediaRequest Method of the controller gets invoked 123 *****************
******************* Requestdetails Is Been Object is been received ***************12345
************** Started creating the Httpost request ********************
************** Finished creating the Http Post request ********************
*********** Before sending the Httppost Request ******************
***************** MediaWorkflowController Received Media Request *******************
java.lang.ClassNotFoundException: com.test.RequestDetails
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:274)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:625)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1612)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1517)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at com.cira.raws.mediawf.api.services.controller.MediaWFController.createMediaRequest(MediaWFController.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
*********** After sending teh Httppost Request ******************

据我了解,该问题是在反序列化对象时发生的。如果我在 RequestDetails 类的两侧保持相同的包层次结构,则代码工作正常。RequestDetails 域对象是否必须具有相同的包层次结构?如果没有,如何解决我面临的问题?

最佳答案

要反序列化对象,您需要在两个实例中具有相同的类。这包括类的包名称。

正如您所说,软件包是不同的,那么您看到的失败很容易理解。

关于java - 使用 Apache HttpClient Api 时 Java 对象的反序列化不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30437755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com