vineri, 12 ianuarie 2018

Java: Jetty with String Boot

Final Project structure:




Step 1: From STS create a new project. (New > Spring Starter Project).





Step 2: Update pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.blogspot.georgelache</groupId>
<artifactId>web-jetty</artifactId>
<version>0.0.1-SNAPSHOT</version>


<name>demo</name>
<description>Demo project for Spring Boot with Jetty</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>

### Please note that the support for release > 1.5.5.RELEASE is broken. This version worked for me
<version>1.5.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>

### Exclude default web server (Tomcat)
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

###Add dependency to Jetty
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>


Step 3: Edit your start-up class

package com.blogspot.georgelache;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoJettyApplication {

public static void main(String[] args) {
SpringApplication.run(DemoJettyApplication.class, args);
}
}

Step 4 (optional): Add your context listener

package com.blogspot.georgelache;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.stereotype.Component;

@Component
public class AppServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("*** contextInitialized");
}

@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("*** contextDestroyed");
}
}

Step 5: Create a test rest controller

package com.blogspot.georgelache.web;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloJetty {
@RequestMapping("/hello")
public String getHello() {
return "Hello";
}

}


Step 6: Update application.property in order to customize the server 

#Startup port
server.port=8866
# The Context Path 
server.contextPath=/jetty

#Jetty Settings
server.compression.enabled: true
server.compression.min-response-size: 1
server.jetty.acceptors=2


Step 7: Run your application (as Java Application)


Niciun comentariu:

Trimiteți un comentariu