2012-12-17 137 views
1

可以讓REST服務與Hessian服務一起工作嗎?帶REST,Java的Hessian Webservice Spring

爲了詳細說明我有這個接口:

@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服務以及的想法這個接口。

使用這個接口的黑森州,因爲它是在春天拋出了一個錯誤,如下所示:

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與REST工作,或者我錯過配置的一個小線,使魔術發生?

回答

1

我建議你;

(醫生後,如果人們問it..I'm真的不知道你需要什麼,我不想做一個額外的TL片段會提供)將業務邏輯放入一個標有@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,所以我不能提供有關如何用粗麻布做事的細節。

+0

我看到...一個額外的圖層可能足以讓Web服務樣式使用通用服務。嘗試實現這個附加層後,我會回來。 –

+0

額外的層次做到了。感謝您的建議。 –