Spring Boot Deployed in Tomcat gives 404 but works Stand-alone - java

I have been testing Spring Boot with embedded Tomcat for about a month now to build a REST API. Everything was working fine. We now want to deploy the API in a separate development environment which has a couple of other (non-Spring) applications running on a Tomcat container.
I made the changes specified in Converting a Spring Boot JAR Application to a WAR using Maven and Spring Boot Docs.
The deployment goes well (logs are fine, no errors) and looking at Tomcat management i see my application deployed. However, when I attempt to access http://localhost:8080/sophia/users in curl I get 404.
Any help is much appreciated.
UPDATE:
Here are my logs:
Netbeans:
NetBeans: Deploying on Apache Tomcat 8.0.17
profile mode: false
debug mode: false
force redeploy: true
In-place deployment at /home/bugz/workspace/pdcore/sophiaserver/target/sophia-server-1.0.0-SNAPSHOT
Deployment is in progress...
deploy?config=file%3A%2Ftmp%2Fcontext1845402702541504208.xml&path=/sophia
OK - Deployed application at context path /sophia
Start is in progress...
start?path=/sophia
OK - Started application at context path /sophia
Tomcat:
INFO 10:47:52:703 org.springframework.boot.context.embedded.ServletRegistrationBean - Mapping servlet: 'dispatcherServlet' to [/sophia/*]
INFO 10:47:54:042 org.springframework.boot.SpringApplication - Started application in 8.285 seconds (JVM running for 12087.301)
22-Jan-2015 10:47:54.060 INFO [http-nio-8080-exec-99] org.apache.catalina.startup.HostConfig.deployDescriptor Deployment of configuration descriptor /home/bugz/workspace/server/apache-tomcat-8.0.17/conf/Catalina/localhost/sophia.xml has finished in 12,091 ms
And in sophia.xml for Catalina localhost:
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" docBase="/home/bugz/workspace/pdcore/sophiaserver/target/sophia-server-1.0.0-SNAPSHOT" path="/sophia"/>
I've tried accessing
http://localhost:8080/sophia/users
http://localhost:8080/sophia-server-1.0.0-SNAPSHOT/users (name of WAR)
The first returns a 404 but with the CORS information from my CORS filter bean. The second return 404 without CORS information (which indicates the application has started and is configured but I do not seem to have access to the Controllers).

When running an application the path to call consists of a couple of parts.
The first is the base URL on which the application is deployed, in your case that is /sophia.
The second is the servlet mapping of the DispatcherServlet in your case that is /sohpia/*.
The third is the mapping of the controller inside the DispatcherServlet, in your example that is /users.
All those things combined create the URL /sophia/sophia/users.
The difference between the deployment as a WAR is that you included a separate URL to deploy on, when running as a jar it, by default, is deployed to / (the root).
You could fix it by putting /sophia as the server.context-path in the application.properties and map the DispatcherServlet to /* or /. That will in both situations give you the URL you want (and expected).

check java -version means the if you complied war in java 8 and tomcat is running on java 7 then it doesn't work.

Have you tried extending the SpringBootServletInitializer and overriding the configure method? As stated here
The entry point for jar file is different [Main Class] when compared to running your app as war file inside a container.
Hope this helps.

check the java version
version that exists in pom.xml must be the same that the version installed on the server
openjdk 11 works fine for me
https://adoptopenjdk.net/

Related

Want to change the context path in Spring boot application in an external Tomact server version 9

I want to deploy a Spring boot application in an external Tomcat server version 9. I am able to deploy it and working the endpoints also. But properties I have set in application.properties file those are not working. Like server.servlet.context-path=/myapp is is not working instead the context path which I am getting is http://localhost:8080/myapp-0.0.1-SNAPSHOT/api/ping.
I am using 2.3.10.RELEASE and Java 1.8 and Tomcat version 9.0.46 Can anyone please help me out with this.
But everything is perfectly working on embedded tomcat. Thanks in advance and any suggestion, comment is highly appreciated.
Can anyone please help me with how I can do this - My war file name would be myapp-0.1.war but the context path of the application would be like this localhost:8080/myapp/api/ping
Use finalName property in your build file (pom.xml for maven)
<finalName>myapp</finalName>
When you run a Spring Boot application in an external servlet container, the server.* properties do not apply.
If you are willing to change the naming convention you can drop a WAR file named myapp##0.1.war in the $CATALINA_BASE/webapps directory and benefit from parallel deployment (cf. parallel deployment).
If you want to stick to your naming convention, you can create a folder for your WAR files (e.g. $CATALINA_BASE/webapps-available) and create a deployment descriptor $CATALINA_BASE/conf/<engine_name>/<host_name>/<context_path>.xml (in your case probably $CATALINA_BASE/conf/Catalina/localhost/myapp.xml) with the following content:
<Context docBase="${catalina.base}/webapps-available/myapp-0.1.war" />

Project doesnt deploy to localhost root but deploys to localhost/projecttitle

Alternative title: Unable to find [main.SearchServlet], mapping my servlet in Tomcat whilst using annotations
I'm trying to migrate my web application from java netbeans glassfish jdk 1.8 to eclipse tomcat jdk 1.7.
I keep getting a 405 then 404 error when I try to perform my POST by pressing a button on my loaded index jsp.
javax.naming.NameNotFoundException: Name [main.SearchServlet/annj] is not bound in this Context. Unable to find [main.SearchServlet].
I thought that setting a <context root>/</context root> in web.xml would fix this. It did move my loading page from localhost/animelist/index and localhost/animelist down one level to localhost/ and localhost/animelist however it did not fix my error.
As it stands, my project does not run at http://localhost:8080/ or http://localhost:8080/index.jsp . It DOES run at http://localhost:8080/animelist1/ and http://localhost:8080/animelist1/index.jsp
The project works fine in my netbeans setup so my question is what do i need to do for my servlets to be mapped properly in tomcat?
Someone said that " Likely the only problem you have is that the application deployment in your secondary Eclipse isn't working properly yet. As in: the servlet class file is not deployed to the server at all." I'm not sure how to go about fixing this (or googling for it).
I have my classes instanced with
#EJB
private AnnJAXB annj;
and servlet with
#WebServlet(name = "SearchServlet", urlPatterns = { "/search" })
and like i said it all works in netbeans setup.
so do i have to do something with tomcat config or application properties, since deployment should be done automatically? why isn't mainClass.SearchServlet being found?
thankyou for reading. i am near the end i feel . it has been 15 hours trying to deploy this to a host and it is exhausting.

Switch java spring maven project from linux to windows

I am new to spring development so i am stuck here. Is there any configuration while switching spring + maven project from linux to windows. I am running on tomcat server 7. The project is initially developed in linux. I have moved all the file from linux to wondows. While running on server, i get error The requested resource (/myproject/login/) is not available. Do i need to make any changes or add add in configuation.
Thanks in advance.
I think you have missed something.
Try this.but i'am not sure this is working or not.
Tomcat, by default invoker servlet disabled (commented out in the web.xml file). You have to create a 'servlet' and a 'servlet-mapping' entry in your web.xml.
Once you do, you can get rid of the "servlet/" part of your url.
Check out the following URL for more information regarding the invoker servlet:
http://faq.javaranch.com/view?InvokerServlet

How do configure grails to preserve your grails.app.context variable when generating a war?

With grails you change the rootContext of the application when running it using grails run-app by using the grails.app.context variable. However, when generating a war grails allows the servlet container to set the context by leaving the configuration out of web.xml. Is there any way to preserve the config in web.xml?
No, there is no way. As said earlier, grails.app.context is used for run-app where we know the embedded servlet container is either Tomcat or Jetty.
In case of a war, the application will not know which container will the war be deployed. It is upto the container to set the context root which vary from one to another.

Setting context for development server [playframework 2.1.3]

I am using Java / Play Framework 2.1.3.
In production, I build a war using the plugin, serve it in tomcat, and tell apache to redirect /myapp to my tomcat app.
In development, I start the server with:
play debug run
And the root for my dev instance is "/".
Now I would like to specify the context for my development instance. For example, I'd like the route to be /myapp but in the routes file I'd like root to still be "/", etc.
How do I do this?
In Play 2.1.x you can specify the context in your application.conf file.
application.context="/myapp"
...or you can do what I do and just run a local Apache instance and do the same thing as you do in prod :)

Categories