简体   繁体   English

泽西找不到资源:404错误

[英]Jersey doesn't find Resources : 404 error

I am working on a simple RESTful Api using Jersey 2.10 and running on Tomcat 8 . 我正在使用Jersey 2.10Tomcat 8上运行一个简单的RESTful Api。

this is my Application class : 这是我的应用程序类:

package com.manager.api.application;

import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;

@ApplicationPath ("api")
public class Application extends ResourceConfig {

    public Application () {
        packages ("com.manager.api.resources");
    }   
}

My Resources package contains : 我的资源包包含:

an interface : Resources.java 接口: Resources.java

package com.manager.api.resources;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

public interface Resources {

    @GET
    @Produces (MediaType.APPLICATION_JSON)
    public JsonObject getList ();

    @GET
    @Path ("value=/{id}")
    @Produces (MediaType.APPLICATION_JSON)
    public JsonObject get (String id);

    @POST
    @Path ("value=/{data}")
    @Consumes (MediaType.APPLICATION_JSON)
    public void post (JsonObject data);

    @PUT
    @Path ("value=/{data}")
    @Consumes (MediaType.APPLICATION_JSON)
    public void put (JsonObject data);

    @DELETE
    @Path ("value=/{id}")
    public void delete (String id);
}

An abstract class : ResourcesImpl.java which implements the Resources interface 一个抽象类: ResourcesImpl.java ,它实现Resources接口

package com.manager.api.resources;

public abstract class ResourcesImpl implements Resources {  
}

And finally a resource class which extends ResourcesImpl.java : UserResources.java 最后是扩展ResourcesImpl.java的资源类: UserResources.java

package com.manager.api.resources;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.manager.api.dao.UserDao;

@Path ("value=/users")
public class UserResources extends ResourcesImpl {

    private UserDao user = new UserDao ();

    @GET
    @Path ("value=/test")
    @Produces (MediaType.TEXT_PLAIN)
    public String Test () {
        return "Test";
    }

    @Override
    public JsonObject getList () {
        return user.getList ();
    }

    @Override
    public JsonObject get (String id) {
        return user.get (id);
    }

    @Override
    public void post (JsonObject data) {
        user.post (data);
    }

    @Override
    public void put (JsonObject data) {
        user.put (data);
    }

    @Override
    public void delete(String id) {
        user.delete (id);
    }
}

and my web.xml contains only a <display-name> Tag : 而我的web.xml仅包含<display-name>标记:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee  http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
        id="WebApp_ID" version="3.1">

    <display-name>manager-api</display-name>
</web-app>

I run my project successfully but I get 2 problems : 我成功运行了项目,但遇到两个问题:

The following warnings have been detected: WARNING: A HTTP GET method, public JsonObject com.manager.api.resources.UserResources.get(java.lang.String), should not consume any entity. 已检测到以下警告:警告:HTTP GET方法(公共JsonObject com.manager.api.resources.UserResources.get(java.lang.String))不应消耗任何实体。 : Which I find strange since I put @Produces (MediaType.APPLICATION_JSON) and didn't put a @Consumes for this method. :自从我放入@Produces (MediaType.APPLICATION_JSON)以来,并没有为该方法放入@Consumes ,这使我感到奇怪。

Finally the big problem is the 404 error I get when I type : http://localhost:8080/manager-api/api/users/test when it should print Test as text. 最后,最大的问题是当我键入以下内容时出现404错误http://localhost:8080/manager-api/api/users/test当它应该将Test打印为文本时。

Do you have any idea about what is the reason behind those 2 errors ? 您是否知道这2个错误背后的原因是什么? Thank you. 谢谢。

Make sure your pom.xml has servlet 3.0 and not 2.0. 确保您的pom.xml具有Servlet 3.0,而不是2.0。 Also try giving an absolute path instead of a relative one in your @ApplicationPath annotation (such as "/api"). 也可以尝试在@ApplicationPath批注(例如“ / api”)中提供绝对路径,而不是相对路径。 Also maybe your package declaration is incorrect for the resourceconfig? 也可能您的包声明对于resourceconfig不正确吗?

Refer to this if you have more troubles: How to set up JAX-RS Application using annotations only (no web.xml)? 如果您有更多麻烦,请参考此文章: 如何仅使用注释(不使用web.xml)设置JAX-RS Application?

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

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