带有JAX-RS 2和Tomcat 9的RESTful Java [英] RESTful Java with JAX-RS 2 and Tomcat 9

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

问题描述

我正在用JAX-RS 2测试RESTful服务.这是我的项目和我的课程.

I am testing RESTful service with JAX-RS 2. Below is my project and my classes.

@ApplicationPath("/test/*")
public class MyApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> clazz = new HashSet<>();
        clazz.add(UserResource.class);
        System.out.println("getClass");
        return clazz;
    }

资源类,如:

@Path("/user")
public class UserResource {

    @GET
    @Produces("application/xml")
    public Response getUser() {
        return Response.created(URI.create("/dd")).build();
    }

    @GET
    public Response getDefaultUser() {
        System.out.println("getDefaultUser");
        return Response.created(URI.create("/dd")).build();
    }
}

当我使用http://localhost:8082/dt/test/user测试服务时,出现404错误.

When I test the service using http://localhost:8082/dt/test/user, I got 404 error.

推荐答案

您所拥有的只是JAX-RS API jar.这只是一个规格"罐.它仅具有规范中定义的接口和类.但是,这背后没有引擎.该规范仅包含我们可以编程的 API ,但这取决于提供引擎的规范的实现.几个实现分别是Jersey和RESTEasy.

All you have is the JAX-RS API jar. This is only a "specifications" jar. It only has the interfaces and classes defined in the specification. But there is no engine behind this. The specification only contains the APIs that we can program against, but it's up to an implementation of the specification that provides the engine; a couple implementations being Jersey and RESTEasy.

话虽这么说,JAX-RS是Java EE规范的一部分,所以任何完全符合Java EE的服务器都将这种实现作为其内部库的一部分.但是Tomcat 不是完全兼容的Java EE服务器.它只是一个servlet容器,仅实现EE规范的一小部分,例如servlet和JSP.因此,如果要在Tomcat中使用JAX-RS,则应添加实现.

That being said, JAX-RS is a part of the Java EE specification, so any fully Java EE compliant server will have this implementation as a part of it's internal library. But Tomcat is not a fully compliant Java EE server. It is only a servlet container, that implements only a small part of the EE specification, like servlets and JSP. So if you want to use JAX-RS in Tomcat, you should add an implementation.

您可以使用的一种实现方式是 Jersey .如果单击链接,则可以下载"JAX-RS 2.0 RI软件包".将它们添加到您的项目中,它应该可以工作.

One implementation you can use is Jersey. If you click the link, you can download the "JAX-RS 2.0 RI Bundle". Add these to your project, and it should work.

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

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