简体   繁体   English

带有REST和Java Spring的Hessian Web服务

[英]Hessian Webservice with REST, Java Spring

Is it possible to have a REST service work hand-in-hand with a Hessian service? REST服务能否与Hessian服务并存?

To elaborate I have this interface: 详细来说,我有这个界面:

@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);

}

I created this interface with the idea of it being a purely RESTful service but changes in requirements needs me to implement a Hessian Service as well. 我创建此接口的初衷是将其作为纯RESTful服务,但需求的变化也需要我实现Hessian服务。

Using this interface for Hessian as it is has Spring throwing up an error as follows: 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)

which clearly tells me that either I'm integrating REST and Hessian incorrectly or these two cannot exist in the same service at the same time... 显然这告诉我要么我错误地集成了REST和Hessian,要么这两个不能同时存在于同一服务中...

So can't Hessian work with REST or am I missing a little line of configuration to make magic happen? 那么Hessian不能与REST一起工作吗?或者我缺少一些配置来使魔术发生吗?

(Extra snippets will be provided if people ask for it..I'm not really sure what you need and I don't want to make a tl;dr post.) (如果有人要求,会提供额外的摘要。我不太确定您需要什么,也不想发表tl; dr帖子。)

I would recommend that you put the business logic in the serivce in a class marked with @Service which can then have a front end that a controller for http or some other adapter for hessian or RMI or SOAP or whatever is being thrown at you. 我建议您将业务逻辑放在标记为@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(....)
  }
}

I have not used Hessian so I can't offer details about how to do things with hessian. 我没有使用过粗麻布,所以无法提供有关如何使用粗麻布的细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM