paulc4 / mvc-content-neg

Demonstrates use of Content Negotiation with Spring MVC
39 stars 27 forks source link

<mvc:interceptors> not working #2

Open user20161119 opened 10 years ago

user20161119 commented 10 years ago

i use the follow mvc config,but the mvc:interceptores not working,i google it,and most answers told that not use

 <mvc:annotation-driven /> 

i want to the internal reason,thanks

<context:component-scan base-package="com">
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <mvc:interceptors>
       <mvc:interceptor>
          <mvc:mapping path="/"/>
          <bean id="ucInterceptor" class="com.cyberdis.hands.component.uc.UCInterceptor"/>
       </mvc:interceptor>
    </mvc:interceptors>
    <!-- Enables the Spring MVC @Controller programming model -->
     <mvc:annotation-driven /> 

    <!-- @ResponseBody -->
    <bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean
                    class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/plain;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <bean class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean" id="cnManager">
        <property name="favorPathExtension" value="true"/>
        <property name="ignoreAcceptHeader" value="true" />
        <property name="defaultContentType" value="text/html" />
        <property name="mediaTypes">
            <map>
                <entry key="html" value="text/html" />
                <entry key="json" value="application/json" />
                <entry key="xml" value="application/xml" />
                <entry key="msg" value="application/message" />
            </map>
        </property>
    </bean>
user20161119 commented 10 years ago

and i can not useing @ResponseBody annotation to convert object to json,i add jackjson jar in my classpath like in your pom.xml

@RequestMapping(value="test",produces={"application/json"})
    public @ResponseBody FBUser json(HttpServletRequest request,HttpServletResponse response){
        /*
        CustomerUser cusUser = null;

        if(cusUser == null){
            cusUser = new CustomerUser();
            cusUser.setStrCusrName("hailiang");
        }
        request.setAttribute("userInfo", cusUser);*/
        FBUser fu = new FBUser();
        fu.setUserEmail("abc");
        return fu;
    }
user20161119 commented 10 years ago

i use:

<property name="favorPathExtension" value="false"/>

and your demo project is not working