0

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;
        }
    }
Sid
  • 4,893
  • 14
  • 55
  • 110
rahul
  • 383
  • 1
  • 8
  • 20
  • How do you accessing the controller, what is the url you are using? – imprezzeb Nov 11 '18 at 04:41
  • localhost:8080/test/transaction and "test" is context path which i have added to Tomcat webmodule – rahul Nov 11 '18 at 04:42
  • 1
    Remove `@ResponseBody` and print Exception if exists instaed of `return null` – Ori Marko Nov 11 '18 at 04:46
  • I have tried without Responsebody as well but it didnt work. The problem is with the controller or some configuration. It doest not even hit the controller. – rahul Nov 11 '18 at 04:50
  • 1
    It sounds like this is the solution: [The origin server did not find a current representation for the target resource or is not willing to disclose that one exists](https://stackoverflow.com/questions/43931383/the-origin-server-did-not-find-a-current-representation-for-the-target-resource) – paulsm4 Nov 11 '18 at 04:50
  • No In my case i could access JSP and even url pattern seems fine to me. – rahul Nov 11 '18 at 04:57
  • can you try adding `@RequestMapping("/")` on Controller class – Ryuzaki L Nov 11 '18 at 05:17
  • 2
    If you want to develop rest service it will be much more easier in spring boot ! – Avijit Barua Nov 11 '18 at 05:46
  • If you're starting from scratch, then definitely go with Spring Boot; you can generate a complete working project in one click at https://start.spring.io. This avoids many, many headaches involved with configuring projects. The container style, configuration style (XML; use JavaConfig), JSP (use Thymeleaf), and injection style (avoid field injection) in your tutorial are all obsolete. – chrylis -cautiouslyoptimistic- Nov 11 '18 at 06:07
  • You should use spring-boot rather then this old implementation. In springboot everything would be autocomfigure you just have to provide the dependency and run the application. – GauravRai1512 Nov 11 '18 at 06:07

2 Answers2

1

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.

Himanshu
  • 164
  • 4
0

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.

Ajish
  • 81
  • 3