Search code examples
spring-mvcjacksonbuildpath

Spring 3.2 Application stopped serving JSON, now sending 406 error


Ok, I am working on a Spring 3.2 project that was serving RESTful JSON.

I have this web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
    <servlet-name>saltCityWifi</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/servlet-config.xml</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>saltCityWifi</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
<display-name>Salt City Wifi</display-name>
</web-app>

and this servlet-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.saltcitywifi" />
<mvc:annotation-driven />
</beans>

Here is my controller:

package com.saltcitywifi.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.saltcitywifi.model.HotSpot;
import com.saltcitywifi.service.HotSpotService;
import com.saltcitywifi.service.HotSpotServiceImpl;

@Controller
public class HotSpotController {
@RequestMapping(value="/hotSpot", method = RequestMethod.GET)
public @ResponseBody List<HotSpot> getHotSpots() {  
    HotSpotService hotSpotService = new HotSpotServiceImpl();
    List<HotSpot> spots = hotSpotService.getAllHotSpots();      
    return spots;
}
}

Everything was working fine on Friday, came back to do a little bit of learning on this project and am suddenly getting a 406 error "The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers."

I think that the strongest possibility is that for some reason the Jackson asl dependency is no longer being included on my build path -- but I see the jar inside of the maven dependency folder when I click configure build path.

Any help with this is greatly appreciated (and I have tried clean / update project and read tons of these similar issues deal with here to no avail.).


Solution

  • UGH. Finally figured it out after I posted the question. I was navigating to localhost:8080/myapp/my-route.html.

    Navigating to localhost:8080/myapp/my-route.json OR localhost:8080/myapp/my-route works correctly as expected.

    The 406 error wasn't a problem with my application, it was a problem with my request -- I don't have any html responses available, but I am accepting the requests for .html in my web.xml file when I declare the url pattern /*.

    Hopefully someone sees this and avoids hours of tracking down potential dependency and build path issues and just learns to make the proper request.