0

Controller

package com.example;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/ws")
public class Controller {
    @RequestMapping(value="/req", method = RequestMethod.GET,produces="application/json")
    TestBean readBookmarks() {
    TestBean bean = new TestBean("Test", 25);
    return bean;
}
}

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/j2ee" 
xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd 
http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">
<display-name>Spring MVC Application</display-name>
  <servlet>
    <servlet-name>App</servlet-name>
    <servlet-class>  org.springframework.web.servlet.DispatcherServlet</servlet-class
<load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
    <servlet-name>App</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

App-Servlet.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:p="http://www.springframework.org/schema/p"
   xmlns:context="http://www.springframework.org/schema/context"
   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">

<context:component-scan base-package="com.example"/>
</beans>

When I try to hit http://localhost:8080/ws/req on browser it says HTTP Status 404.

URL's are registered as observed in logs:

Jun 07, 2017 10:35:39 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:MathWebApp' did not find a matching property.
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/8.0.44
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          May 10 2017 17:21:09 UTC
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         8.0.44.0
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Mac OS X
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            10.12.4
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          x86_64
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_111-b14
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         /Users/kegupta/Documents/Framework/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         /Users/kegupta/Desktop/apache-tomcat-8.0.44
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/Users/kegupta/Documents/Framework/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/Users/kegupta/Desktop/apache-tomcat-8.0.44
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=/Users/kegupta/Documents/Framework/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=/Users/kegupta/Desktop/apache-tomcat-8.0.44/endorsed
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
Jun 07, 2017 10:35:39 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /Users/kegupta/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
Jun 07, 2017 10:35:39 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Jun 07, 2017 10:35:39 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Jun 07, 2017 10:35:39 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Jun 07, 2017 10:35:39 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 480 ms
Jun 07, 2017 10:35:39 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jun 07, 2017 10:35:39 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.44
Jun 07, 2017 10:35:40 AM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Jun 07, 2017 10:35:40 AM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Jun 07, 2017 10:35:40 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'App'
Jun 07, 2017 10:35:40 AM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'App': initialization started
Jun 07, 2017 10:35:40 AM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'App-servlet': startup date [Wed Jun 07 10:35:40 IST 2017]; root of context hierarchy
Jun 07, 2017 10:35:40 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/App-servlet.xml]
Jun 07, 2017 10:35:40 AM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/ws/req] onto handler 'controller'
Jun 07, 2017 10:35:40 AM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/ws/req.*] onto handler 'controller'
Jun 07, 2017 10:35:40 AM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/ws/req/] onto handler 'controller'
Jun 07, 2017 10:35:40 AM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'App': initialization completed in 617 ms
Jun 07, 2017 10:35:40 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Jun 07, 2017 10:35:40 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Jun 07, 2017 10:35:40 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1514 ms

I am stuck since very long. Any help would be greatly appreciated.

Azzabi Haythem
  • 2,318
  • 7
  • 26
  • 32
keshav gupta
  • 25
  • 2
  • 5
  • Try to close this tag `` in web.xml. Rename `App-Servlet.xml` to `App-servlet.xml` and retry – harshavmb Jun 07 '17 at 05:41
  • Are you using the springboot way of running via main? or deploying the war of the application in an tomcat? – Rohan S Raju Jun 07 '17 at 05:46
  • @RohanSRaju deployed using Tomcat in Eclipse. – keshav gupta Jun 07 '17 at 06:05
  • @harshavmb didn't work – keshav gupta Jun 07 '17 at 06:05
  • what didn't work? Can you be more specific? Are you using tomcat/jetty container for deployment? Can you check the deployment directory and verify whether classes are present? – harshavmb Jun 07 '17 at 06:06
  • @harshavmb classes are present in /MathWebApp/WEB-INF/classes/com/example/. Also, I am using Tomcat container for deployment in eclipse. servlet-class tags are closed and name of file is App-servlet.xml didn't worked for me. – keshav gupta Jun 07 '17 at 06:18
  • which url are you checking? – harshavmb Jun 07 '17 at 06:21
  • You should validate by checking `localhost:8080/App/ws/req` – harshavmb Jun 07 '17 at 06:29
  • @harshavmb I validated by checking localhost:8080/App/ws/req as well as localhost:8080/ws/req. none of them are working – keshav gupta Jun 07 '17 at 06:33
  • Can you add `` to `App-servlet.xml` and try? Since, you named your class `Controller` I guess spring isn't creating controller bean for you. – harshavmb Jun 07 '17 at 06:39
  • @harshavmb adding above line to App-servlet.xml causes following exception: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping': Initialization of bean failed; nested exception is java.lang.IllegalStateException: Cannot map handler 'myController' to URL path [/ws/req]: There is already handler of type [class com.example.Controller] mapped. – keshav gupta Jun 07 '17 at 06:54
  • Remove the `@Controller` annotation on the Controller class and retry – harshavmb Jun 07 '17 at 06:55
  • @harshavmb removing RestController not registering URL's and didn't help. – keshav gupta Jun 07 '17 at 07:02
  • Aww.,! Got it. Sorry. You retain `@RestController` annotation, refactor the class to MyRestController and also change the class name in `App-servlet.xml` file – harshavmb Jun 07 '17 at 07:05
  • @harshavmb changed class name to MyWebController didn't work. Also I haven't class in App-servlet.xml. Do we need to give this? – keshav gupta Jun 07 '17 at 07:56
  • Yes, we have to – harshavmb Jun 07 '17 at 07:58
  • Follow [this](https://stackoverflow.com/questions/44349820/spring-mvc-mapping-error/44350788?noredirect=1#comment75797069_44350788). I've created this for many people suffering like you. It is for normal @Controller, so you can exclude view part. Rest everything remains same – harshavmb Jun 07 '17 at 08:01

2 Answers2

0

From the logs i can see a war name App is being deployed. Try using this url :

http://localhost:8080/App/ws/req 

Or

http://localhost:8080/<deployed tomcat war name>/ws/req 
Rohan S Raju
  • 426
  • 3
  • 7
  • 20
0
TestBean readBookmarks() {
TestBean bean = new TestBean("Test", 25);
return bean;

This function seems to be lacking @ResponseBody and also TestBean (maybe a custom class) is not something which is serializable by Spring. Can you please try sending String instead of TestBean -- That should work.

Additionally, it is possible that the deployment directory differs from your web.xml config (i.e. something other than App). You can try loading the http://localhost:8080/{Dir}/ws/req where Dir == deployment directory (!= 'App').

The above 2 things should probably resolve your issue.