gpt4 book ai didi

java - 灰熊 Jersey 吞咽异常

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:08:40 24 4
gpt4 key购买 nike

我在最后使用快速入门原型(prototype)构建 Jersey Moxy 服务。我的代码工作正常,我可以返回一些 JSON。然而,在我开发的过程中,如果我犯了一个错误,比如说请求处理程序有一个不受支持的类型,我将得到一个空的 500 响应,这使得调试变得困难。例如,如果我用 @XmlElementRef 错误地装饰了一个属性,我将得到如下响应:

$ 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

服务器将表现得好像没有任何问题:

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...

我尝试使用日志配置文件:

-Djava.util.logging.config.file=log.conf

这会产生大量输出,但仍未显示任何类型的异常。

我已经尝试查看 Grizzly 配置,但找不到关闭优雅错误处理的方法。理想情况下,我希望服务器抛出异常。对我缺少的东西有什么建议吗?

这是我的主要代码:

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;
}
}
}

代码与此处的示例几乎相同:

https://github.com/jersey/jersey/tree/2.2/examples/json-moxy/src/main/java/org/glassfish/jersey/examples/jsonmoxy

我使用的完整原型(prototype)生成:

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

非常感谢收到的建议。

最佳答案

异常没有传播到 Grizzly 层,所以它应该被 Jersey 记录下来。我还没有找到您必须启用哪种记录器,但看起来自定义 ExceptionMapper 可以提供帮助。

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import org.glassfish.grizzly.utils.Exceptions;

@Provider
public class MyExceptionMapper implements
ExceptionMapper<WebApplicationException> {
@Override
public Response toResponse(WebApplicationException ex) {
return Response.status(500).entity(Exceptions.getStackTraceAsString(ex)).type("text/plain")
.build();
}
}

关于java - 灰熊 Jersey 吞咽异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18634067/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com