Allinone51
Allinone51

Reputation: 624

Spring REST service "No mapping found for HTTP request with URI"

I'm having a strange issue with my rest server using Spring. I didn't found any solution to my problem in other questions.

Everything works fine, I have multiple mappings to different controller methods and they all work. I made a new mapping to another URI and I can't make it work, I always get the 404 not found error when trying to access it.

Here is some of my code:

Restcontroller:

@RequestMapping(value = "/addToken/{token}/imei/{imei}", method = RequestMethod.POST)
    public boolean setUserToken(@PathVariable("token") String token, @PathVariable("imei") String imei) {
        boolean result = false;
        try {
            result = DbConnector.setToken(imei,token);
        } catch (Exception e) {
            System.out.println("Couldn't set token to database");
        }
        return result;
    }

This is the method that doesn't work and I don't know why, when I call base_url/addToken I get no error but when I add anything after it I get 404 error.

Web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 <display-name>SpringServiceSample</display-name>
 <servlet>
    <servlet-name>rest</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>rest</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

rest-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="
        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.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
 <context:component-scan base-package="controllers" /> <!-- Spring auto scans all elements in base package and all its child packages for Controller servlet -->

<!-- <mvc:annotation-driven> -->
<!--      <mvc:message-converters> -->
<!--             <bean class="org.springframework.http.converter.StringHttpMessageConverter"/> -->
<!--             <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> -->
<!--    </mvc:message-converters> -->
<!-- </mvc:annotation-driven> -->

<mvc:annotation-driven />
  </beans>

Thanks in advance for your help.

Upvotes: 2

Views: 2411

Answers (1)

Kunal Surana
Kunal Surana

Reputation: 659

setUserToken method serves only post request. you need to submit form with post request to this URL or change

@RequestMapping(value = "/addToken/{token}/imei/{imei}", method = RequestMethod.GET)

Upvotes: 1

Related Questions