Jonathan
Jonathan

Reputation: 121

Access spring boot application by url

I'm traying do a simple spring boot application but a have problems for access to the route of this app. My app is very simple. This the pom:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
  </parent>
  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.7</java.version>
    </properties>
  <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
  </dependencies>

  <build>
    <finalName>demospringboot</finalName>
    <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <executable>true</executable>
                </configuration>
            </plugin>
        </plugins>
  </build>
  <packaging>jar</packaging>

This is the main class

@SpringBootApplication
@ComponentScan(basePackages = "main")
@EnableAutoConfiguration
public class ApplicationMain {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationMain.class, args);
    }
}

And this the de Controller class

@RestController
public class ControllerMain {

    @RequestMapping("/springboot/hello")
    public @ResponseBody String home() {

        return "Hello World";
    }
}

Well, when I execute the application in localhost all run right, I can put http://localhost:8080/springboot/hello in a browser and the result appears. My problems starts when I put the jar generated on a production server. This server is a typical VPS (Virtual Private Server) with Plesk that have a Apache server and Tomcat integrated. First I stop the tomcat service of the server because my spring boot application have a Tomcat embedded and this produces conflicts between the two Tomcat servers. After I upload de jar file to the server and execute it with:

java -jar demospringboot.jar

I see a trace that shows how the application is deployed but when I put in a browser something like http://myserverdomain/springboot/hello I get a 404 error. My server have mod_jk installed to have apache server and tomcat server serving static files and java applications at the same time, for this I add this configuration to the httpd.conf into a virtualhost

<IfModule mod_jk.c>
        JkMount /springboot ajp13
        JkMount /springboot/* ajp13
</IfModule>

When I added this configuration the error 404 pass to error 503 Service Unavailable. What are the steps for deploy a spring boot application jar with tomcat embedded in a VPS as I have?

Upvotes: 2

Views: 5426

Answers (1)

Jonathan
Jonathan

Reputation: 121

Finally I have found a solution to my problem. First I have added this to my application.properties file:

server.port=8082
server.use-forward-headers=true
server.contextPath=/mainstay

After I have created this class

@Controller
public class ServletConfig {
    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();

        Connector ajpConnector = new Connector("AJP/1.3");
        ajpConnector.setProtocol("AJP/1.3");
        ajpConnector.setPort(9090);
        ajpConnector.setSecure(false);
        ajpConnector.setAllowTrace(false);
        ajpConnector.setScheme("http");
        tomcat.addAdditionalTomcatConnectors(ajpConnector);

        return tomcat;
    }
}

I have compiled my application and I have upload the jar to my server. With this, I have edited the workers file of my server and I have added this:

worker.list=ajp13,spring
worker.ajp13.port=9008
worker.ajp13.host=localhost
worker.ajp13.type=ajp13

worker.spring.port=9090
worker.spring.host=localhost
worker.spring.type=ajp13

And I have edited the httpd.conf file with this

<IfModule mod_jk.c>
    JkMount /mainstay spring
    JkMount /mainstay/* spring
</IfModule>

I have restart the httpd service I could access to my application

Upvotes: 4

Related Questions