Aliy
Aliy

Reputation: 217

Rest controller not being called in Spring

I'm using Spring Rest controller for my Restful calls. I'm having Spring 4.3.x version of JAR's. When I run the project itself, the index.jsp is not getting called. I've not configured anything in xml because I'm using annotation method. Here are my files.

P.S : I'm not using Maven, its a dynamic web project and all Spring JAR's (Webmvc, web, core, context, beans) are added to build path.

I've followed http://viralpatel.net/blogs/spring-4-mvc-rest-example-json/

AppConfig

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "net.ifg.spring")
public class AppConfig {

}

AppInitializer

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class[] getRootConfigClasses() {
    return new Class[] { AppConfig.class };
}

@Override
protected Class[] getServletConfigClasses() {
    return null;
}

@Override
protected String[] getServletMappings() {
    return new String[] { "/" };
}

}

CustomerDAO

public class CustomerDAO {

// Dummy database. Initialize with some dummy values.
private static List<Customer> customers;
{
    customers = new ArrayList();
    // Add customers here
}


public List list() {
    return customers;
}


}

CustomerRestController

@RestController
public class CustomerRestController {


@Autowired
private CustomerDAO customerDAO;


@GetMapping("/customers")
public List getCustomers() {
    return customerDAO.list();
}

}

Web.xml

 <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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>IFG</display-name>
<welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>

Why its not able to hit the URL http://localhost:8080/IFG/customers? There should be the issue with AppInitializer file.

Any ideas would be greatly appreciated.

Upvotes: 1

Views: 2739

Answers (6)

Aliy
Aliy

Reputation: 217

Just checked that it was due to javax.servlet.ServletException: Failed to instantiate WebApplicationInitializer class. This I've fixed by adding commons-logging-1.2.jar, spring-aop, spring-expression jars. Atleast I'm able to point to the right method.

Upvotes: 0

pleft
pleft

Reputation: 7905

After http://localhost:8080 you have to give your app name prior calling the controller. Lets say your app name is myApp, so the url should be:

http://localhost:8080/myApp/customers

provided you use the controller in your question, if you have added to your controller @RequestMapping("/IFG") as per other answers suggested then you have to change the url to:

http://localhost:8080/myApp/IFG/customers

EDIT

I see in your AppInitializer class you are returning null from getServletConfigClasses(). I believe that the AppConfig.class should be returned there.

@Override
protected Class[] getServletConfigClasses() {
    return new Class[] { AppConfig.class };
}

Upvotes: 0

yvoytovych
yvoytovych

Reputation: 871

Just try access http://localhost:8080/customers (without /IFG), just copied that project locally, on my tomcat, and it works, without running mvn tomcat7:run.

Your setup in web.xml <display-name>IFG</display-name> is not the application context path by which to access.

From the docs:

display-name

The optional display-name element specifies the Web application display name, a short name that can be displayed by GUI tools.

Upvotes: 0

Nikolas
Nikolas

Reputation: 44368

You have to specify IFG in a request mapping. The current link according to your mapping should be http://localhost:8080/customers. Add the @RequestMapping annotation specifying the path.

@RestController
@RequestMapping("/IFG")
public class CustomerRestController

Upvotes: 1

NikNik
NikNik

Reputation: 2301

To make it work, you should add:

@RestController
@RequestMapping("/IFG")

and check the number of port: 8080 (it could be 9080 or whatever)

Upvotes: 0

Mehraj Malik
Mehraj Malik

Reputation: 15854

@RestController
@RequestMapping("/IFG")
public class CustomerRestController {


@Autowired
private CustomerDAO customerDAO;


@GetMapping("/customers")
public List getCustomers() {
    return customerDAO.list();
}

}

Now hit the url like below :

localhost:port/IFG/customers

Upvotes: 0

Related Questions