是否可以让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;博士。)
答案 0 :(得分:1)
我建议你把业务逻辑放在一个标有@Service的类中,然后可以有一个前端,一个控制器用于http或一些其他适配器用于粗麻布或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所以我不能提供有关如何用粗麻布做事的细节。