如何在grizzly服务器控制台上获取HTTP请求日志?

时间:2018-09-10 09:46:04

标签: jersey jax-rs grizzly

按照Jersey用户指南上的教程,我创建了一个基于jax-rs的小型REST Web应用程序。这是启动服务器的主要类。

public class Main {
// Base URI the Grizzly HTTP server will listen on
public static final String BASE_URI = "http://localhost:8080/myapp/";

/**
 * 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.example package
    final ResourceConfig rc = new ResourceConfig().packages("com.example");

    // 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.shutdownNow();
   }
}

GET请求的资源方法处理程序:

@Path("myresource")
public class MyResource {

/**
 * Method handling HTTP GET requests. The returned object will be sent
 * to the client as "text/plain" media type.
 *
 * @return String that will be returned as a text/plain response.
 */
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getIt() {
    return "Got it!";
   }
}

当我运行服务器时,我会在控制台上看到它:

"C:\Program Files\Java\jdk1.8.0_171\bin\java.exe" ... 
 Sep 10, 2018 11:51:55 AM org.glassfish.grizzly.http.server.NetworkListener 
 start
 INFO: Started listener bound to [localhost:8080]
 Jersey app started with WADL available at 
 http://localhost:8080/myapp/application.wadl
 Hit enter to stop it...
 Sep 10, 2018 11:51:55 AM org.glassfish.grizzly.http.server.HttpServer start
 INFO: [HttpServer] Started.

当我通过邮递员向http://localhost:8080/myapp/myresource发送GET请求时,状态为200 OK,并带有“知道了!”。作为响应正文,但没有任何内容记录在服务器控制台中。

这可能是一个非常基本的问题,但是如何实现它,以便每次将HTTP请求发送到灰熊服务器时,请求和响应都记录在控制台上?我真的很感谢任何帮助。

0 个答案:

没有答案