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服务。

Spring将此接口直接用于Hessian时会引发如下错误:

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帖子。)

最佳答案

我建议您将业务逻辑放在标记为@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(....)
  }
}


我没有使用过粗麻布,所以无法提供有关如何用粗麻布做事的详细信息。

10-08 11:19