简体   繁体   English

带有Jersey&Grizzly的JAX-RS:500个请求的所有请求均失败

[英]JAX-RS with Jersey & Grizzly: 500 Request failed for all requests

Hi 你好

I need to develop simple web service on java. 我需要在Java上开发简单的Web服务。 I'm new to java tech and based on several articles decided to use JAX-RS (Jersey) with embedded http server (Grizzly2), because it looks suitable for building REST services and deployment seems to be trivial. 我是Java技术的新手,并且根据几篇文章决定将JAX-RS(Jersey)与嵌入式http服务器(Grizzly2)结合使用,因为它看起来很适合构建REST服务,并且部署似乎很简单。

In my dev environment all works perfect (using IntllijIdea). 在我的开发环境中,所有工作都完美无缺(使用IntllijIdea)。

But when I try to deploy on test server every request returns "500 Internal Error" (even /application.wadl) 但是,当我尝试在测试服务器上进行部署时,每个请求都返回“ 500 Internal Error”(甚至/application.wadl)

Here more info: 这里更多信息:

Simple resource 简单的资源

@Path("resource")
public class SpeechRecognition {
    @GET
    public Response test() {
        Logger.getGlobal().log(Level.INFO, "resource test");
        return Response.ok("success", MediaType.TEXT_PLAIN).build();
    }
}

Simple resource config 简单的资源配置

public class SimpleApiServer extends ResourceConfig {
    public SimpleApiServer() {
        register(MultiPartFeature.class);
        register(LoggingFilter.class);
        register(JacksonFeature.class);

        property(ServerProperties.TRACING, "ALL");
        property(ServerProperties.TRACING_THRESHOLD, "VERBOSE");

        packages("com.mydomain");
    }
}

Simple app start 简单的应用启动

public static void main(String[] args) throws IOException {
    final HttpServer server = GrizzlyHttpServerFactory.createHttpServer("http://0.0.0.0:80", new SimpleApiServer());

    System.out.println(String.format("Jersey app started with WADL available at "
            + "%sapplication.wadl\nHit enter to stop it...", serverUri.toString()));
    System.in.read();
    server.stop();
}

Exception mapping 异常映射

@Provider
public class AppExceptionMapper implements ExceptionMapper<Throwable> {
    @Override
    public Response toResponse(Throwable ex) {
        Logger.getGlobal().log(Level.WARNING, "exception response");

        ex.printStackTrace();

        return Response.status(500).entity(Exceptions.getStackTraceAsString(ex)).type("text/plain")
            .build();
    }
}

What is happening 怎么了

For every request I see in console 对于控制台中看到的每个请求

- that grizzly accepts it (prints all information about headers etc.) - and that resource code is executed ("resource test" is printed) -灰熊接受(打印有关标题的所有信息等)-执行资源代码(打印“资源测试”)

And nothing more. 仅此而已。

In postman I receive "500 Request Failed" with no trace from Jersey in headers (though I specified property(ServerProperties.TRACING, "ALL");) 在邮递员中,我收到“ 500请求失败”,头中没有来自泽西岛的跟踪(尽管我指定了属性(ServerProperties.TRACING,“ ALL”);)

No information printed in server console about any exceptions. 服务器控制台中未打印任何异常的信息。

I have an idea, that something bad happens when jersey tries to pass response to grizzly. 我有一个主意,当球衣试图传递对灰熊的反应时,会发生一些不好的事情。

Does anyone has any thoughts about where should I look to troubleshoot this problem? 是否有人对我应该在哪里解决此问题有任何想法? I really stuck with it. 我真的坚持下去。

And I really don't want to rewrite everything (not this simple ill example, but lots more) to traditional java servlet-oriented code ('cause it's another piece of tech to learn about) 而且我真的不想将所有内容(不是这个简单的例子,而是更多)重写为传统的面向Java servlet的代码(因为这是另一种需要学习的技术)

PS 聚苯乙烯

Test server - is the docker container on azure linux VM. 测试服务器-是Azure Linux VM上的Docker容器。 Requests are proxied by nginx to exposed docker port. Nginx将请求代理到公开的docker端口。 Got web-site on Rails using the same scheme, which works fine. 使用相同的方案在Rails上获得网站,效果很好。

When grizzly prints info about request it prints incorrect hostname. 当灰熊打印有关请求的信息时,它将打印不正确的主机名。 Ie actual host name is test.api.mydomain.com and grizzly writes GET http://test.api:80/resource 即实际的主机名是test.api.mydomain.com,并且灰熊写着GET http://test.api:80 / resource

Ok. 好。
It was the Jersey version mismatch in dev and test environments. 这是开发和测试环境中的Jersey版本不匹配。

Remote debugging (with IntellijIdea ) showed, that in process of forming response 远程调试 (使用IntellijIdea )显示,正在形成响应

Exception: AbstractMethodError: javax.ws.rs.core.Response.getStatusInfo()Ljavax/ws/rs/core/Response$StatusType;

is thrown, wich led me to this post. 被抛出,这使我到了这个职位。
After fixing the dependencies all works fine. 修复依赖关系后,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM