jersey
Utilizzando Spring Boot con Jersey
Ricerca…
Semplice applicazione con Spring Boot e Jersey
Spring Boot è un framework bootstrap per le applicazioni Spring. Ha anche il supporto perfetto per l'integrazione con Jersey. Uno dei vantaggi di questo (dal punto di vista di un utente di Jersey) è che si ha accesso al vasto ecosistema di Spring.
Per iniziare, crea un nuovo progetto Maven standalone (non wepapp). Possiamo anche creare una webapp, ma per questa guida useremo solo un'app standalone. Una volta creato il progetto, aggiungi quanto segue al tuo pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Abbiamo solo bisogno di due dipendenze. Uno per il modulo Jersey Spring Boot e un altro per un server Tomcat incorporato. Il plugin che useremo per eseguire l'applicazione per testare.
Dopo averlo aggiunto, aggiungi le seguenti classi al progetto.
com/example
|
+-- GreetingApplication.class
+-- JerseyConfig.class
|
+ com/example/services
| |
| +-- GreetingService.class
| +-- NiceGreetingService.class
|
+ com/examples/resources
|
+-- GreetingResource.class
GreetingApplication.class
Questa è la classe di bootstrap (molto semplice)
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GreetingApplication {
public static void main(String[] args) {
SpringApplication.run(GreetingApplication.class, args);
}
}
JerseyConfig.class
Questa è la classe di configurazione di Jersey
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.stereotype.Component;
@Component
@ApplicationPath("/api")
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
packages("com.example");
}
}
GreetingService.class
e NiceGreetingService.class
public interface GreetingService {
public String getGreeting(String name);
}
import org.springframework.stereotype.Component;
@Component
public class NiceGreetingService implements GreetingService {
@Override
public String getGreeting(String name) {
return "Hello " + name + "!";
}
}
GreetingResource
Questa è la classe di risorse in cui metteremo Spring inject the GreetingService
.
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.service.GreetingService;
@Path("greeting")
public class GreetingResource {
private GreetingService greetingService;
@Autowired
public GreetingResource(GreetingService greetingService) {
this.greetingService = greetingService;
}
@GET
public String getGreeting(@QueryParam("name") String name) {
return this.greetingService.getGreeting(name);
}
}
E questo è tutto. Ora possiamo eseguire l'applicazione. Prendi un terminale ed esegui il seguente comando dalla radice del progetto.
mvn spring-boot:run
L'applicazione dovrebbe richiedere alcuni secondi per iniziare. Ci sarà un po 'di registrazione, e vedrai un po' di arte ASCII primaverile. Dopo quell'arte, dovrebbero essere circa 30 righe o più di registrazione, quindi dovresti vedere
15.784 seconds (JVM running for 38.056)
Ora l'app è stata avviata. Se usi cURL puoi provarlo con
curl -v 'http://localhost:8080/api/greeting?name=peeskillet'
Se sei su Windows, usa le doppie virgolette sull'URL. Se non stai usando cURL, digita semplicemente nel browser. Dovresti vedere il risultato
Hello peeskillet!
Potresti notare che la richiesta richiede alcuni secondi per la prima richiesta effettuata. Questo perché Jersey non è completamente caricato all'avvio dell'app. Possiamo cambiarlo aggiungendo un file application.properties
nella cartella src/main/resources
. In quel file aggiungi quanto segue:
spring.jersey.servlet.load-on-startup=1