使用Jersey的REST with Java(JAX-RS)中的欢迎页面 [英] Welcome page in REST with Java (JAX-RS) using Jersey

查看:74
本文介绍了使用Jersey的REST with Java(JAX-RS)中的欢迎页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jersey实现一个Restful Web Service.我想将index.jsp显示为欢迎页面.

I am implementing a Restful Web Service using Jersey. I want to show index.jsp as welcome page.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Rest Page</title>
    </head>
    <body>
        <h1>Rest is working!</h1>
    </body>
</html>

当我在web.xml中使用此代码时,它工作正常:

It works fine when I use this code in my web.xml:

<servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/whatever/*</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

问题在于用户模式如下:

The problem is when the user-pattern is like this:

<url-pattern>/*</url-pattern>

除欢迎页面外,其他所有内容均正常运行.感谢您的帮助.

Everything works fine except welcome page. I appreciate Any help.

推荐答案

我找到了另一种方法.除了使用index.jsp,我还可以使用类似的类:

I found an alternative way to do this. instead of using index.jsp, I can use a class like:

@Path("/")
public class Hello {

    // This method is called if HTML is request
    @GET
    @Produces(MediaType.TEXT_HTML)
    public String sayHtmlHello() {
       return "<html> " + "<title>" + "Rest Page" + "</title>"
          + "<body><h1>" + "REST is Working!" + "</body></h1>" + "</html> ";
}

在web.xml中,我不需要使用:

In web.xml I do not need to use:

<welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

它在以下情况下可以正常工作

And it works fine with:

<url-pattern>/*</url-pattern>

这篇关于使用Jersey的REST with Java(JAX-RS)中的欢迎页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆