Issue Details (XML | Word | Printable)

Key: SPR-6163
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Critical Critical
Assignee: Juergen Hoeller
Reporter: Ken Egervari
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
Spring Framework

Using ignoreAcceptHeader and defaultContentType together causes problems.

Created: 29/Sep/09 03:55 PM   Updated: 13/Oct/09 06:02 PM   Resolved: 01/Oct/09 06:52 AM
Component/s: SpringWEB
Affects Version/s: 3.0 RC1
Fix Version/s: 3.0 RC2

Time Tracking:
Not Specified

Issue Links:
Duplicate
 

Virtual Machine: Sun JVM - 1.6
Platform: Tomcat - 6.0


 Description  « Hide

Using ignore accept header and default content type causes problems. Here is my bean xml:

{code?xml}

<bean id="contentNegotiatingViewResolver"
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1"/>
<property name="ignoreAcceptHeader" value="true" />
<property name="defaultContentType" value="text/html" />
<property name="mediaTypes">
<map>
<entry key="ftl" value="text/html"/>
<entry key="xml" value="application/xml"/>
<entry key="json" value="application/json"/>
<entry key="atom" value="application/atom+xml"/>
<entry key="rss" value="application/rss+xml"/>
</map>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<property name="contentType" value="application/xml" />
<property name="marshaller">
<bean class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="autodetectAnnotations" value="true"/>
</bean>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="contentType" value="application/json" />
</bean>
</list>
</property>
<property name="viewResolvers">
<list>
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="contentType" value="text/html" />
<property name="order" value="2"/>
<property name="cache" value="false"/>
<property name="prefix" value=""/>
<property name="suffix" value=".ftl"/>
<property name="exposeSpringMacroHelpers" value="true"/>
</bean>
</list>
</property>
</bean>

Here is the exception that is generated:

SEVERE: Servlet.service() for servlet jawbs threw exception
java.lang.UnsupportedOperationException
at java.util.AbstractList.set(AbstractList.java:115)
at java.util.AbstractList$ListItr.set(AbstractList.java:412)
at java.util.Collections.sort(Collections.java:121)
at org.springframework.web.servlet.view.ContentNegotiatingViewResolver.resolveViewName(ContentNegotiatingViewResolver.java:344)
at org.springframework.web.servlet.DispatcherServlet.resolveViewName(DispatcherServlet.java:1105)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1052)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:808)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:726)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:636)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:545)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)


If there is a short-term fix, I'd love to hear it.

Basically what I want to do is have no extension (like /rest/hotels/list) go to text/html and still have the json, xml, etc. views work with the extensions on any/all browsers.

Thanks!



Mario Fusco added a comment - 01/Oct/09 05:46 AM

The problem is that the ContentNegotiatingViewResolver.getMediaTypes() method when a path extension is not available and the ignoreAcceptHeader is set to true and a defaultContentType has been configured returns a list in the following way:

if (defaultContentType != null) { return Collections.singletonList(defaultContentType); }

that, as it has been reported, cannot be sort by the Collections.sort() method invoked in the ContentNegotiatingViewResolver.resolveViewName() one.

I hope this helps


Mario Fusco added a comment - 01/Oct/09 06:02 AM

If you need a temporary but quick and working fix use the following extension of ContentNegotiatingViewResolver and configure it in your application context.
I had the same problem and that solution is working for me.

<code>
import org.springframework.web.servlet.view.*;
import org.springframework.http.*;

import javax.servlet.http.*;
import java.util.*;

public class MyContentNegotiatingViewResolver extends ContentNegotiatingViewResolver {

protected List<MediaType> getMediaTypes(HttpServletRequest request) { List<MediaType> result = super.getMediaTypes(request); if (result.size() == 1) result = Arrays.asList(result.get(0)); return result; }
}
</code>


Juergen Hoeller added a comment - 01/Oct/09 06:52 AM

Thanks for raising this! Fixed in trunk now.

Juergen