Skip to content

Be able to specify a default content type when no extension is used in ContentNegotiatingViewResolver [SPR-6130] #10798

@spring-projects-issues

Description

@spring-projects-issues

Ken Egervari opened SPR-6130 and commented

I'm having a problem where I am trying to get xml, json, html, etc. to work using the content negotiator. It works perfectly... almost. The main problem I'm having is that the default content type is coming up as XML. Here is the dispatcher servlet xml:

<bean id="contentNegotiatingViewResolver"
      class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="order" value="2"/>
    <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="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"/>
        </list>
    </property>
    <property name="viewResolvers">
        <list>
            <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
                <property name="order" value="1"/>
                <property name="cache" value="false"/>
                <property name="prefix" value=""/>
                <property name="suffix" value=".ftl"/>
                <property name="exposeSpringMacroHelpers" value="true"/>
            </bean>
        </list>
    </property>
</bean>

As it stands, mapping to "/" results in Spring trying to get to "/.xml", which is terrible since it really needs to get mapped to what I have in @RequestMapping in a controller (mine points to "index", which in turns should get mapped to "index.ftl" in the view resolver)

Ultimately though, despite the above mentioned problem, having to specify ".ftl" whenever I want a text/html view is somewhat annoying. It's workable mind you... but then there is no change of having a proper index page.

If there is quick workaround, would you please email me? Maybe I set it up incorrectly too... and if that's the case, I apologize for posting this.

Thanks for all of your help!


Affects: 3.0 M4

Reference URL: http://forum.springsource.org/showthread.php?p=261035#post261035

Issue Links:

Referenced from: commits 5faa4d9

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions