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)


marți, 2 ianuarie 2018

Java: Jetty - Using Jetty Maven Plugin

## Install Apache Maven
## No need for jetty -- the plugin will manage all the stuff

#Create war

C:\test\jettyapp> mvn archetype:generate -DgroupId=com.george.test -DartifactId=jetty-plugin-app  -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
C:\test\jettyapp> cd jetty-plugin-app

##Add Jetty Maven plugin - edit pom.xml file

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.george.test</groupId>
  <artifactId>jetty-plugin-app</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>jetty-plugin-app Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>jetty-plugin-app</finalName>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.11.v20160721</version>
</plugin>
</plugins>
  </build>
</project>

## Deploy the app
C:\test\jettyapp\jetty-plugin-app>mvn jetty:run

## Navigate to http://localhost:8080/


Change context path & port


## Edit pom.xml

<build>
    <finalName>jetty-plugin-app</finalName>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.11.v20160721</version>
<configuration>
<webApp>
<contextPath>/testapp</contextPath>
</webApp>
<httpConnector>
<port>8866</port>
</httpConnector>
</configuration>
</plugin>
</plugins>
  </build>



Java: Jetty - Deploy war app - using context file

## Install Apache Maven and jetty

## Create war

## Create context file: Context path. Refers to the location which is relative to the server’s address and represents the name of the web application.
## This context file must have the same name as the WAR file

# Create jetty-app.xml in C:\java\jetty-distribution-9.4.8.v20171121\webapps\ folder

<?xml version="1.0"  encoding="ISO-8859-1"?>

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<!-- access the web app from the URL http://localhost:8080/testapp -->
    <Set name="contextPath">/testapp</Set>
<!-- set the path to the war file -->
    <Set name="war">C:\test\jettyapp\jetty-app\target\jetty-app.war</Set>
</Configure>


## Start jetty server
C:\java\jetty-distribution-9.4.8.v20171121>java -jar start.jar


Java:Jetty - Deploy war app - copy to %JETTY_HOME%\webapps

## Install Apache Maven and jetty

## Create war

mkdir test
cd test
mkdir jettyapp

cd C:\test\jettyapp\

mvn archetype:generate -DgroupId=com.george.test -DartifactId=jetty-app  -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

cd jetty-app

mvn package

cd target

C:\test\jettyapp\jetty-app\target>cp jetty-app.war C:\java\jetty-distribution-9.4.8.v20171121\webapps\

## Start jetty server
C:\java\jetty-distribution-9.4.8.v20171121>java -jar start.jar