Sto costruendo un servizio Jersey Moxy utilizzando l'archetipo quickstart alla fine. Il mio codice funziona bene e posso ottenere alcuni JSON restituiti. Tuttavia, mentre sto sviluppando, se faccio un errore, diciamo che il gestore della richiesta ha un tipo non supportato, riceverò una risposta vuota di 500, il che rende difficile il debug. Ad esempio, se io decorare un attributo in modo non corretto con @XmlElementRef, mi metterò una risposta come:Grizzly Jersey ingoiare le eccezioni
$ curl -i http://localhost:8080/myapp/test
HTTP/1.1 500 Internal Server Error
Date: Thu, 05 Sep 2013 10:27:55 GMT
Connection: close
Content-Length: 0
Il server agirà come se niente fosse:
Sep 5, 2013 11:27:46 AM org.glassfish.grizzly.http.server.HttpServer start
INFO: [HttpServer] Started.
Jersey app started with WADL available at http://localhost:8080/application.wadl
Hit enter to stop it...
Ho provato ad utilizzare un registro di configurazione file con:
-Djava.util.logging.config.file=log.conf
Questo produce un sacco di output, ma ancora non mostra alcun tipo di eccezione.
Ho provato a esaminare la configurazione di Grizzly ma non riesco a trovare un modo per disattivare la gestione degli errori. Idealmente vorrei che il server generasse un'eccezione. Qualche suggerimento su cosa mi manca?
Ecco il mio codice principale:
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.moxy.json.MoxyJsonConfig;
import org.glassfish.jersey.server.ResourceConfig;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
import java.net.URI;
import java.util.*;
public class Main {
// Base URI the Grizzly HTTP server will listen on
public static final String BASE_URI = "http://localhost:8080/";
/**
* Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.
* @return Grizzly HTTP server.
*/
public static HttpServer startServer() {
// create a resource config that scans for JAX-RS resources and providers
// in com.myapp package
final ResourceConfig rc = new ResourceConfig().packages("com.myapp").registerInstances(new JsonMoxyConfigurationContextResolver());
// create and start a new instance of grizzly http server
// exposing the Jersey application at BASE_URI
return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
}
/**
* Main method.
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
final HttpServer server = startServer();
System.out.println(String.format("Jersey app started with WADL available at "
+ "%sapplication.wadl\nHit enter to stop it...", BASE_URI));
System.in.read();
server.stop();
}
@Provider
final static class JsonMoxyConfigurationContextResolver implements ContextResolver<MoxyJsonConfig> {
@Override
public MoxyJsonConfig getContext(Class<?> objectType) {
final MoxyJsonConfig configuration = new MoxyJsonConfig();
Map<String, String> namespacePrefixMapper = new HashMap<String, String>(1);
namespacePrefixMapper.put("http://www.w3.org/2001/XMLSchema-instance", "xsi");
configuration.setNamespacePrefixMapper(namespacePrefixMapper);
configuration.setNamespaceSeparator(':');
return configuration;
}
}
}
Il codice è quasi identica a quella dell'esempio qui:
generazione archetipo completa che ho usato:
mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 \
-DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false \
-DgroupId=com.myapp -DartifactId=yarese-service -Dpackage=com.myapp \
-DarchetypeVersion=2.2
Suggerimenti con gratitudine ricevuto.
"... se I decorare un attributo in modo non corretto con @XmlElementRef ..." Ho avuto lo stesso problema. Anche con il logging rivolto verso l'alto, non ho ricevuto alcuna informazione su esattamente cosa c'era di sbagliato nelle mie annotazioni JAXB. Ho aperto [questo numero di Jersey] (https://java.net/jira/browse/JERSEY-2321). –