jersey
Configurazione di JAX-RS in Jersey
Ricerca…
Filtro CORS Java Jersey per richieste di origine incrociata
@Provider
public class CORSResponseFilter implements ContainerResponseFilter {
public void filter(
ContainerRequestContext requestContext,
ContainerResponseContext responseContext
) throws IOException {
MultivaluedMap<String, Object> headers = responseContext.getHeaders();
headers.add("Access-Control-Allow-Origin", "*"); //Allow Access from everywhere
headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
headers.add("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
}
}
Si noti che Access-Control-Allow-Origin è utile solo alle risposte OPTIONS.
Configurazione java Java
Questo esempio illustra come configurare Jersey in modo da poter iniziare ad usarlo come framework di implementazione JAX-RS per l'API RESTful.
Supponendo che tu abbia già installato Apache Maven , segui questi passaggi per configurare Jersey:
- Crea la struttura del progetto web maven, in terminal (windows) esegui il seguente comando
mvn archetype: generate -DgroupId = com.stackoverflow.rest -DartifactId = jersey-ws-demo -DarchetypeArtifactId = maven-archetype-webapp -DinteractiveMode = false
Nota: per supportare Eclipse, utilizzare il comando Maven: mvn eclipse: eclipse -Dwtpversion = 2.0
- Vai alla cartella in cui hai creato il tuo progetto di maven, nel tuo pom.xml, aggiungi le dipendenze richieste
<dependencies>
<!-- Jersey 2.22.2 -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<!-- JSON/POJO support -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependencies>
<properties>
<jersey.version>2.22.2</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
- In Web.xml, aggiungi il seguente codice
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<!-- Service or resources to be placed in the following package -->
<param-value>com.stackoverflow.service</param-value>
</init-param>
<!-- Application configuration, used for registering resources like filters -->
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.stackoverflow.config.ApplicationConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Url mapping, usage-http://domainname:port/appname/api/ -->
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
- La classe
ApplicationConfig
public class ApplicationConfig extends ResourceConfig {
public ApplicationConfig() {
register(OtherStuffIfNeeded.class);
}
}
Va anche notato che se si vuole andare senza web.xml, basta semplicemente liberarsene e aggiungere @ApplicationPath("/api")
sopra la classe ApplicationConfig
.
@ApplicationPath("/api")
public class ApplicationConfig extends ResourceConfig {
public ApplicationConfig() {
// this call has the same effect as
// jersey.config.server.provider.packages
// in the web.xml: it scans that packages for resources and providers.
packages("com.stackoverflow.service");
}
}
- Costruisci e distribuisci il tuo progetto di maven.
- Ora puoi impostare le tue classi Java RESTful webservice (JAX-RS) per usare i barattoli di Jersey.
Modified text is an extract of the original Stack Overflow Documentation
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow