- 
                Notifications
    
You must be signed in to change notification settings  - Fork 38.8k
 
Description
Matt Fletcher opened SPR-14299 and commented
I am running on Spring/Spring MVC 3.2.11. I have a controller as follows:
@Controller
@RequestMapping("/cost")
public class CostGuideHomeController
{
   @RequestMapping(value="", method = RequestMethod.GET, produces = "!application/json")
   public String costGuideHome(final HttpServletRequest request, final ModelMap model)
   {
//...
   }
}I have view resolvers as follows:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
      <property name="order" value="5"/>
      <property name="mediaTypes">
         <map>
            <entry key="json" value="application/json"/>
         </map>
      </property>
      <property name="defaultViews">
         <list>
            <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
         </list>
      </property>
   </bean>
   <bean id="redirectViewResolver" class="com.**.spring.mvc.view.RedirectViewResolver">
      <description>
         Looks for view names with the 'redirect:' or 'permanent-redirect:'
         and performs either a 302 or 301 redirect, respectively.
      </description>
      <property name="order" value="10" />
	</bean>
   <bean id="tilesResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
      <description>Attempts to map a view name to a tiles definition.</description>
      <property name="order" value="15" />
      <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
	</bean>
   <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <description>Look up view name in /WEB-INF/jsp directory</description>
      <property name="order" value="25" />
      <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
      <property name="prefix" value="/WEB-INF/jsp/"/>
      <property name="suffix" value=".jsp"/>
   </bean>
   <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <description>Default view resolver that attempts to look up the view name as a JSP.  This is really only needed
      because historically we didn't put jsps under /WEB-INF/jsp/*.  All spring controllers should be returning views
      that live in WEB-INF.  This view resolver handles all the old code that doesn't</description>
      <property name="order" value="30" />
      <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
   </bean>
I have been seeing requests to this controller with Accept=application/json. I want to disallow these. So I change the request mapping to the following:
@RequestMapping(value="", method = RequestMethod.GET, produces = "!application/json")This does disallow requests with Accept=application/json, however it also appears to disallow requests which don't include a Accept header. Very unexpected behavior. Appears to be because the ProducesRequestCondition#getAcceptedMediaTypes returns a singleton list of MediaType.ALL. So it matches everything. Since the requestmapping has a negation, the subsequent check in ProducesRequestCondition#getMatchingCondition thinks that matching the request is not good... and I get back a 404 since no other handlers are configured for this endpoint.
I don't think we have a ContentNegotiationManager directly configured, and Spring appears to be using a default one that just looks at request headers.
Affects: 3.2.17