user3342812
user3342812

Reputation: 343

Spring 4 Rest service 404 Error

I am learning spring 4 mvc and creating a simple web app. I have a Spring 4 backend with REST endpoints to hit from my AngularJS front end. The problem is whenever I hit these endpoints from my front end the request returns a 404.

Here's my Spring configuration files

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>


  <!-- Spring MVC -->
  <servlet>
      <servlet-name>mvc-dispatcher</servlet-name>

      <servlet-class>
          org.springframework.web.servlet.DispatcherServlet
      </servlet-class>

      <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
      <servlet-name>mvc-dispatcher</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

mvc-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="
      http://www.springframework.org/schema/beans    
      http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.2.xsd">

  <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
      <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
      <value>.jsp</value>
    </property>
  </bean>

</beans>

loginController.java

package controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;



@RestController
public class loginController {



    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public String listAllUsers() {
        return "test";

    }
}

I've tried changing my configuration a lot of different ways but nothing seems to work. What could be causing the problem here?

Upvotes: 1

Views: 2175

Answers (1)

Krzysztof Krasoń
Krzysztof Krasoń

Reputation: 27516

As it was written in the comment by @Ali, you should have

<mvc:annotation-driven/>
<context:component-scan base-package="controller" />

in your mvc-dispatcher-servlet.xml

See http://docs.spring.io/autorepo/docs/spring/4.2.x/spring-framework-reference/html/mvc.html for details.

Upvotes: 2

Related Questions