23

I have built an application using Spring with Eclipse IDE. When I launch the project from Eclipse IDE everything is fine but when I package the maven project as a war file and deployed to separate tomcat I have this issue

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

This is a configuration snippet from my xml file

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

I am trying to access this controller

@RequestMapping(value = {"/welcome", "/"})
    public String defaultPage() {
            return "Web Service data successfuly consumed";


    }

anyone with an idea why this is failing on deployed to tomcat?

borisbn
  • 4,988
  • 25
  • 42
Blaze
  • 2,269
  • 11
  • 40
  • 82
  • Solved the problem by creating fresh maven project from the bottom up then re-add existing packages and xml files. Initially what I created was a dynamic web project that I converted to maven project via configuration settings of eclipse – Blaze Apr 25 '17 at 11:57
  • check the url mapping,clean and re-build the project and update maven again it will work – Rahul Jain Dec 12 '19 at 05:18
  • For Anyone who was unable to follow Tadele's answer like me, here is a link that at least solved my issue. My Java build Path entries were not added as defined here. https://crunchify.com/how-to-fix-java-lang-classnotfoundexception-org-springframework-web-servlet-dispatcherservlet-exception-spring-mvc-tomcat-and-404-error/ – vibhor vaish Feb 22 '21 at 20:11

15 Answers15

13

I struggled with this problem many times.

The solution I am currently using is weather the webapp(or the folder where you kept the views like jsp) is under deployment assembly.

To do so Right click on the project > Build Path > Configure Build path > Deployment Assembly > Add(right hand side) > Folder > (add your jsp folder. In default case it is src/main/webapp)

You could also get this error after you did everything correct but on the JSP you put the anchor tag the old fashion(I am adding this incase if it help anybody else with the same issue).

I had the following syntax on the jsp. <a href="/mappedpath">TakeMeToTheController</a> and I kept seeing the error mentioned in the question. However changing the tag into the one shown below solved the issue.

<a href=" <spring:url value="/mappedpath" /> ">TakeMeToTheController</a>
Tadele Ayelegn
  • 4,126
  • 1
  • 35
  • 30
11

I've received the same error when working in a Spring Boot Application because when running as Spring Boot, it's easy to do localhost:8080/hello/World but when you've built the artifact and deployed to Tomcat, then you need to switch to using localhost:8080/<artifactName>/hello/World

Ryan D
  • 1,023
  • 11
  • 16
  • 1
    +1, I feel like existing tutorials on Tomcat don't stress this enough: lost some hours today because of the artifact name. Anyways thanks for helping me out! – tb96 Dec 15 '20 at 19:21
  • Where can i find the ? should i set it in tomcat plugin in pom file or what? – Moath Thawahreh Jan 14 '22 at 11:50
  • meaning the name of what your project built. So if the project is called FooBar and the build results in FooBar.war that gets deployed to Tomcat, your url looks like localhost:8080/FooBar/hello/world – Ryan D Jan 14 '22 at 16:44
  • Wasted hours trying to figure it out. Cheers! – Mihai Răducanu Feb 08 '23 at 10:44
  • I have added the artificats as well but still getting the same error. what I am doing it i have an springboot application i have created the war for it and deployed it on my tomcat server .. I am getting the error. : - http://localhost:8090/files-converter-1.0.0/hello. my war is : - files-converter-1.0.0.war please help – Gaurav Dangi Apr 27 '23 at 15:20
11

If you are developing spring boot application add "SpringBootServletInitializer" as shown in following code to your main file. Because without SpringBootServletInitializer Tomcat will consider it as normal application it will not consider as Spring boot application

@SpringBootApplication
public class DemoApplication extends *SpringBootServletInitializer*{

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
         return application.sources(DemoApplication .class);
    }

    public static void main(String[] args) {
         SpringApplication.run(DemoApplication .class, args);
    }
}
paytam
  • 327
  • 6
  • 15
JEETHESH KARKERA
  • 193
  • 1
  • 2
  • 11
4

Trying to run a servlet in Eclipse (right-click + "Run on Server") I encountered the very same problem: "HTTP Status: 404 / Description: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists." Adding an index.html did not help, neither changing various settings of the tomcat.

Finally, I found the problem in an unexpected place: In Eclipse, the Option "Build automatically" was not set. Thus the servlet was not compiled, and no File "myServlet.class" was deployed to the server (in my case in the path .wtpwebapps/projectXX/WEB-INF/classes/XXpackage/). Building the project manually and restarting the server solved the problem.

My environment: Eclipse Neon.3 Release 4.6.3, Tomcat-Version 8.5.14., OS Linux Mint 18.1.

2

I was facing the same issue and with some hint from @tadtab 's answer, I was able to figure out a solution for the same problem in my project.

Steps:

1->Follow the steps mentioned in @tadtab's answers.

2->Right Click on the project->Click on Properties->Search for Deployment Assembly.

3->Search whether your folder exists on the screen. (If not, add it).

4->On the screen you will find a 'Deploy Path' column corresponding to your source folder. Copy that path. In my case, it was /views.

enter image description here 5->So basically, in the setPrefix() method, we should have the path at the time of deployment. Earlier I was just using /views in the setPrefix() method, so I was getting the same error. But after, it worked well.

@Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();

        resolver.setPrefix("/WEB-INF/classes/");
        resolver.setSuffix(".jsp");

        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;

    }

The same should be applicable to XML configuration also.

Deep
  • 929
  • 2
  • 16
  • 32
  • this might help further. https://crunchify.com/how-to-fix-java-lang-classnotfoundexception-org-springframework-web-servlet-dispatcherservlet-exception-spring-mvc-tomcat-and-404-error/ – vibhor vaish Feb 22 '21 at 20:12
1

I am also facing the same issue and i resolve it by putting web.xml file and the applicationcontext.xml file in WEB-INF folder.

Hope this helps :)

krati
  • 43
  • 5
1

This issue can even occur when you try to run your project from controller page. Run your project from the jsp page. Go to your jsp page; right-click->Run As->Run on Server. I faced the same issue.I was running my project from the controller page. Run your project from jsp page.

Smitha
  • 21
  • 2
0

If it is maven project do Maven Update will solve the problem - Right Click on Project --> Maven --> Update Project and start your project normally.

Rudy
  • 679
  • 2
  • 9
  • 17
0

solution one: Change the version of apache tomcat (latest one is preferred) (manual process).

solution two: Install latest eclipse IDE and configure the apache tomcat server (internally automatic process i,e eclipse handles the configuration part).

After successful procedure of automatic process, manual process shall be working good.

dferenc
  • 7,918
  • 12
  • 41
  • 49
0

I am facing the error like The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

In Eclipse right click on project and click on Build then try to run as server. It will work fine for me

Community
  • 1
  • 1
0

I was facing same problem.

When I rightclick-> run on server then select my server manually it worked.

Do

Alt+Shift+X

then mannually select your server. It might help.

Elif
  • 345
  • 3
  • 14
0

Almost same problem get resolved by creating a geoexplorer.xml file in /opt/apache-tomcat-8.5.37/conf/Catalina/localhost content of geoexplorer.xml file is

<Context displayName="geoexplorer" docBase="/usr/share/opengeo/geoexplorer" path="/geoexplorer"/>
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
0

Your dispatcher servlet does not where to dispatch the request. Issue is your controller bean is not created/working.

Even I faced the same problem. Then added the following under mvc-config.xml

<mvc:annotation-driven/>
<context:component-scan base-package="com.nsv.jsmbaba.teamapp.controller"/>

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

Hope this helps

Naga
  • 800
  • 7
  • 8
0

Check Java versions That was a problem for me. IDE (Intellij in my case) could launch without the problem, but when I tried to run war inside tomcat docker image app didn't work. The reason was docker image had a different (lower) version compare to the development environment. There were no errors messages indicating any of this.

my-
  • 604
  • 9
  • 17
0

Check to find the root cause by reading logs in the tomcat installation log folder if all the above answers failed.Read the catalina.out file to find out the exact cause. It might be database credentials error or class definition not found.

mumbasa
  • 632
  • 7
  • 11