rahul
rahul

Reputation: 393

Not able to access Spring controller

I am trying to create a Spring RESTful API very a basic application from scratch, but I am not able to access the controller. I could access JSP file but not controller. I have tried with to annotate with @RestController as well but it didn't work. I am running on Tomcat 8.

Error is:

The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. WEB.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>

      <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

dispatcher-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

    <mvc:annotation-driven />
    <context:component-scan
        base-package="com.controller />
    <mvc:default-servlet-handler />

</beans>

My Controller is

    @Controller
public class TransactionControllerImpl{
    @Autowired
    private TransactionService transactionService;

    @RequestMapping(method = RequestMethod.GET, value="/transaction")
    @ResponseBody
    public String getTransactionList() {
        try {
            System.out.println("from controller");
            return "HEllow rahul";//transactionService.getTransactionList();

        }
        catch (Exception e) {
            return null;
        }
    }

Upvotes: 0

Views: 239

Answers (2)

Himanshu
Himanshu

Reputation: 164

If you are creating application context separately, you should provide context param and value as location of your context.xml file.

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/application-context.xml</param-value>
</context-param>

For your error the controller is not accessible,it might be due to :-

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

check if you have written correct base-package name or try using

<context:component-scan base-package="..package name..">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

Hope it helps.

Upvotes: 1

Ajish
Ajish

Reputation: 96

Remove listener from web.xml because you have only one context xml. If you want to load multiple context xml add listener and context param.

Upvotes: 0

Related Questions