将氛围与Spring MVC集成

时间:2013-12-17 07:31:59

标签: spring-mvc push-notification atmosphere

我想在Spring MVC项目中提供推送通知,使用针对所有浏览器的JDK 1.6。我跟着this post,最后决定和Atmosphere一起去。 对于服务器发送的事件,我的服务器控制器是(source):

@Controller
public class AtmosphereController {

    @RequestMapping(value="/getTime", method=RequestMethod.GET)
    @ResponseBody
    public void websockets(final AtmosphereResource atmosphereResource) {

        final HttpServletRequest  request = atmosphereResource.getRequest();
        final HttpServletResponse response = atmosphereResource.getResponse();

        atmosphereResource.suspend();

        final Broadcaster bc = atmosphereResource.getBroadcaster();
        bc.scheduleFixedBroadcast(new Callable<String>() {

            public String call() throws Exception {

                return (new Date()).toString();
            }
        }, 10, TimeUnit.SECONDS);
    }
}

在跑步的时候,我得到了org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.atmosphere.cpr.AtmosphereResource]: Specified class is an interface。 在解决方案中,我得到了this relevant post。我将它添加到我的dispatcher-servlet.xml:

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
            </list>
        </property>
    </bean>

<mvc:annotation-driven>
    <mvc:argument-resolvers>
        <bean id= "atmosphereResource"
            class="org.atmosphere.cpr.AtmosphereResourceImpl" />
    </mvc:argument-resolvers>
</mvc:annotation-driven>

这样做也会导致新的错误:

[cvc-complex-type.2.1: Element 'mvc:annotation-driven' must have no character or element information item [children], because the type's content type is empty.]

我也试过this。请帮我在Spring Controller中注入AtmosphereResource。我是否还需要更新 web.xml 或其他配置文件以使其正常工作或我缺少哪些部分。请帮忙! 还请评论提供服务器端事件功能的其他替代方法。提前谢谢!

2 个答案:

答案 0 :(得分:2)

您的参数解析器必须是这样的类:

public class AtmosphereArgumentResolver implements HandlerMethodArgumentResolver {

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        HttpServletRequest httpServletRequest= webRequest.getNativeRequest(HttpServletRequest.class);
        return Meteor.build(httpServletRequest).getAtmosphereResource();
    }

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
            return AtmosphereResource.class.isAssignableFrom(parameter.getParameterType());
    }

}

试试这个:

<mvc:annotation-driven>
    <mvc:argument-resolvers>
        <bean class="com.yourpackage.AtmosphereArgumentResolver" />
    </mvc:argument-resolvers>
</mvc:annotation-driven>

答案 1 :(得分:1)

尝试Atmosphere 2.1.0-RC1并遵循此document。您需要做的就是将environment-spring.jar添加到您的依赖项中。

- Jeanfrancois