带有Tomcat的Jersey JAX-RS ResourceConfig [英] Jersey JAX-RS ResourceConfig with Tomcat

查看:114
本文介绍了带有Tomcat的Jersey JAX-RS ResourceConfig的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以正常工作的基本REST Web服务,但是我有一个问题.这是一个简短的代码片段.

I have a basic REST web service that works but I do have a question. Here is a brief code snip.

package com.my.app;
import org.glassfish.jersey.server.ResourceConfig;
import javax.ws.rs.ApplicationPath;

@ApplicationPath("api")
public class RestApplication extends ResourceConfig {
    RestApplication() {
        packages("com.my.app");
    }
}

还有

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>The name of my service!</display-name>
</web-app>

我一直在 https://jersey.java.net/apidocs/2.25.1/jersey/org/glassfish/jersey/server/ResourceConfig.html ,但我还没有找到设置Tomcat的方法显示名称或版本.现在,我可以在web.xml中设置这些参数,效果很好,但我宁愿在扩展ResourceConfig并完全摆脱web.xml的类中设置参数.这可能吗?还是我应该坚持使用web.xml?任何建议将不胜感激.

I have been digging around in the Jersey documentation at https://jersey.java.net/apidocs/2.25.1/jersey/org/glassfish/jersey/server/ResourceConfig.html and I haven't found a way to set the Tomcat display name or version. Now I can just set those parameters in the web.xml and that works just fine but I would rather set the parameters in my class that extends ResourceConfig and get rid of the web.xml altogether. Is this possible or should I just stick with using the web.xml? Any suggestions would be greatly appreciated.

推荐答案

类是Jersey API的一部分,并提供高级功能来简化JAX-RS组件的注册,例如在提供的类路径或一组包名称中扫描根资源和提供程序类.

The ResourceConfig class is part of the Jersey API and provides advanced capabilities to simplify registration of JAX-RS components, such as scanning for root resource and provider classes in a provided classpath or a in a set of package names.

它扩展了 Application 来自JAX-RS API的类.有关 Application 类,请参阅此答案.

web.xml是基于servlet容器的应用程序的部署描述符.它指示servlet容器必须加载哪些类(servlet,过滤器和侦听器),应在上下文中设置哪些属性,等等.

The web.xml is a deployment descriptor for servlet container based applications. It instructs the servlet container which classes (servlets, filters and listeners) must be loaded, which properties should be set in the context, etc.

从Servlet 3.0开始,您甚至不需要web.xml即可进行简单的部署.大多数配置,例如注册servlet,过滤器和侦听器,都可以通过注释.

Since Servlet 3.0, you don't even need the web.xml for simple deployments. Most of the configurations, such as registering servlets, filters and listeners can be done via annotations.

但是,如果要设置Web应用程序的<display-name>,仍然需要web.xml.到目前为止,还没有可以替换该标签的注释.

However, the web.xml is still necessary if you want to set the <display-name> of your web application. So far, there's no annotation that can replace that tag.

Apache Tomcat 8与Servlet 3.1规范兼容(有关更多信息,请参见 Tomcat文档详细信息),因此您的web.xml可能类似于:

Apache Tomcat 8 is compatible with the Servlet 3.1 specification (check the Tomcat documentation for more details), so your web.xml can be like:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <display-name>Web Application Name</display-name>
</web-app>

这篇关于带有Tomcat的Jersey JAX-RS ResourceConfig的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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