Search code examples
javaspringspring-mvcservletshttp-status-code-406

Spring mvc servlet-mapping and 406 http core


I'm working with Spring mvc 4 and glassfish 4 and I had some problems with ajax calls to return object as json, with @ResponseBody. Then I found a proper solution HTTP Status 406. Spring MVC 4.0, jQuery, JSON that it did not totally work on my project I think because my servlet-mapping.

My servlet mapping was:

   <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

But I want also to serve the request like /* to avoid problems with 406 http code so I've tried this:

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

But then the dispatcher also maps the *.jsp and breaks At the end I've tried this:

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

But then the dispatcher maps the *.js and *.css like bootstrap, jquery etc.

And now I ran out of ideas.


Solution

  • At the end I fix it. For one side to fix the 406 http code problem I specify the content type from ajax and I remove the *.htm from the controller.

    And for the other side I had to fix the servlet-mapping and add the resource tag to avoid more problems.

    Now my servlet-mapping is like this:

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    

    And I add this line to the dispatcher-servlet:

    <bean id="viewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver"
              p:prefix="/WEB-INF/jsp/"
              p:suffix=".jsp" />
    
    <mvc:resources mapping="/resources/**" location="/resources/" />