5

I have written a web application using spring mvc 3. It provides a single endpoint that returns JSON. I did having it running successfully using url paramters but now I need to change this to use path variables instead.

I changed my controller

from:

@Controller
public class DataController {

    @Autowired
    private IDataService dateService;

    @RequestMapping(
            value = "/some/data",
            method = RequestMethod.GET,
            produces = "application/json"
    )
    public @ResponseBody Data getDataByCode(@RequestParam String code) {
        return versionService.getDataByCode(code);
    }
}

to:

@Controller
public class DataController {

    @Autowired
    private IDataService dateService;

    @RequestMapping(
            value = "/some/data/{code}",
            method = RequestMethod.GET,
            produces = "application/json"
    )
    public @ResponseBody Data getDataByCode(@PathVariable String code) {
        return versionService.getDataByCode(code);
    }
}

and my web xml to map the url...

from:

    <servlet>
        <servlet-name>dataBycode</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:/config/servlet-config.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>dataBycode</servlet-name>
        <url-pattern>/some/data</url-pattern>
    </servlet-mapping>

to:

    <servlet>
        <servlet-name>dataBycode</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:/config/servlet-config.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>dataBycode</servlet-name>
        <url-pattern>/some/data/*</url-pattern>
    </servlet-mapping>

But I get a 404 for the http request: http://localhost/some/data/1234 Where I know that 1234 exists.

To top it off, the request never even gets to the controller method, so its got to be the web xml url pattern configuration.

As a test I tried changing the url pattern to exactly match a request with a code and it returned JSON as expected, so I know its the url pattern. I just dont know how to fix it.

I have tried:

  1. /
  2. /*
  3. /some/data*
  4. /some/data/*
Jeremy
  • 3,418
  • 2
  • 32
  • 42
  • How your Spring configuration is load ? – fabballe Dec 11 '15 at 12:43
  • Its loaded through xml configuration, do you think that could have an influence on the app working with anything but path variables? If so then I will add that detail to the question. Remember the application functions as expected if I don't use path variable. For example, it works ok with a path parameter as the ```code``` value. – Jeremy Dec 11 '15 at 14:25
  • Indeed spring configuration should not be the problem. I add a answer with some tips. – fabballe Dec 12 '15 at 16:40

2 Answers2

1

Your url-pattern should be:

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

In the second hand, can you try to access http://localhost/some/data/myTest and put a System.out.println in your getDataByCode method to see if the method is called.

If it works I think the problem come from Spring MVC which interprate "1234" as an integer and not a String.

fabballe
  • 761
  • 4
  • 12
  • Hum interesting I will try this out and get back to you – Jeremy Dec 15 '15 at 17:06
  • I changed the url pattern to ```/``` and it seems to work now, I didn't have a problem with the path variable type in the end. – Jeremy Dec 17 '15 at 11:13
  • Weird that I had tried that in the past though, perhaps my configuration has changed since I posted this question that fixed the a deeper issue. – Jeremy Dec 17 '15 at 11:14
0

If your are using Eclipse:

  1. Double click on your Server inside Server View

server view

  1. Go to Modules Tab

enter image description here

  1. check the path for your Application

The final application url = path + some/data/1234

alex
  • 8,904
  • 6
  • 49
  • 75
  • As I said in my question, I know that the path is correct because I can allow access to a specific value in my web.xml url format and it returns the expected data. It's just when I try and use a path variable that the problem occurs and I get a 404. – Jeremy Dec 11 '15 at 14:21