1

REST 服务是否可以与 Hessian 服务协同工作?

详细说明我有这个界面:

@Controller
public interface PhotoServices {

    @RequestMapping(value = "/{user}/{folder_name}/savephoto/", method = RequestMethod.POST)
    public @ResponseBody Object savePhoto(HttpServletRequest request, @PathVariable("user")String user, @PathVariable("folder_name")String folder_name, @ModelAttribute("photo") Photo photo, BindingResult result, SessionStatus session);

    @RequestMapping(value = "/user/getuserphotos/", method = RequestMethod.GET)
    public @ResponseBody Object getUserPhotos(HttpServletRequest request);

    @RequestMapping(value = "/{user}/{folder_name}/photo/{filename}", method = RequestMethod.GET, produces="image/jpg")
    public @ResponseBody byte[] getPhoto(@PathVariable("user")String user, @PathVariable("folder_name")String folder_name, @PathVariable("filename") String filename);

}

我创建这个接口的想法是它是一个纯粹的 RESTful 服务,但需求的变化也需要我实现一个 Hessian 服务。

将这个接口用于 Hessian 时,Spring 会抛出如下错误:

12:24:52,765 ERROR http-bio-8080-exec-1 servlet.DispatcherServlet:457 - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Initialization of bean failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'photoService' bean method 
public java.lang.Object mppiwebservices.photoservices.impl.photo.PhotoServicesImpl.savePhoto(javax.servlet.http.HttpServletRequest,java.lang.String,java.lang.String,mppiwebservices.photoservices.pojo.Photo,org.springframework.validation.BindingResult,org.springframework.web.bind.support.SessionStatus)
to {[/{user}/{folder_name}/savephoto/],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'photoServicesImpl' bean method
public java.lang.Object mppiwebservices.photoservices.impl.photo.PhotoServicesImpl.savePhoto(javax.servlet.http.HttpServletRequest,java.lang.String,java.lang.String,mppiwebservices.photoservices.pojo.Photo,org.springframework.validation.BindingResult,org.springframework.web.bind.support.SessionStatus) mapped.
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1228)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1147)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:836)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

这清楚地告诉我,要么我错误地集成了 REST 和 Hessian,要么这两者不能同时存在于同一个服务中......

所以 Hessian 不能与 REST 一起工作,还是我错过了一些配置来让魔法发生?

(如果人们要求,将提供额外的片段。我不太确定你需要什么,我不想发表 tl;dr 帖子。)

4

1 回答 1

1

我建议您将业务逻辑放在标有@Service 的类中的服务中,然后该类可以有一个前端,该控制器可以用于 http 的控制器或用于 hessian 或 RMI 或 SOAP 的其他适配器或向您抛出的任何东西。

// pure business logic method here no dependency on servlets or any network protocol just pojos in
// pojos out 
@Service
public interface PhotoServices {
   public void doSomething(....)

}

/**
* Restful interface knows about Sevlets request and response http status codes ... etc\
* does web stuff and delegates to the PhotoService
*/
@Controller
public class PhotoWebController {

 @Autowired
 private PhotoService photoSerivce;

  @RequestMapping .... 
  public void doSemething(....)
  {
        this.photoSerivce.doSomething(....)
  }
}




/**
* RPC end point understand how to handle error conditions with Hessian ... etc
* 
*/
 // put whatever annoations you need to put for hessian 
public class PhotoHessianController {

 @Autowired
 private PhotoService photoSerivce;

  // hessian annotations go here
  public void doSemething(....)
  {
        this.photoSerivce.doSomething(....)
  }
}

我没有使用 Hessian,所以我无法提供有关如何使用 hessian 做事的详细信息。

于 2012-12-17T05:11:37.800 回答