GET 返回 404 Not Found in JAX-RS

问题描述

我尝试使用 GET 调用此网址 http://localhost:8080/javaee-1.0/app/recipe/all,但我收到 404 未找到。下面是管理器和web xml和Application类的代码

配方管理器

@Path("/recipe")
@RequestScoped
public class RecipeManager implements RecipeManagerInterface {

    @Inject
    RecipeServiceInterface recipeService;

    @Override
    @Path("/all")
    @Produces(MediaType.APPLICATION_JSON)
    @GET
    public List<RecipeDTO> getRecipes() {
        return recipeService.getRecipes();
    }

    @Override
    @DELETE
    @Path("/")
    @Consumes(MediaType.APPLICATION_JSON)
    public void deleteRecipe(RecipeDTO recipeDTO) {
        recipeService.deleteRecipe(recipeDTO);
    }

    @Override
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Path("/")
    public void saveRecipe(RecipeDTO recipeDTO) {
        recipeService.saveRecipe(recipeDTO);
    }

    @Override
    @GET
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/{id}")
    public List<RecipeDTO> getRecipeById(@PathParam("id") Long id) {
        return recipeService.getRecipeById(id);
    }

    @Override
    @GET
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/")
    public List<RecipeDTO> getRecipeByIdAndTitle(@QueryParam("id") Long id,@QueryParam("title") String title) {
        return recipeService.getRecipeByIdAndTitle(id,title);
    }
}

web.xml

<web-app>
    <servlet>
        <display-name>My JAX-RS Servlet</display-name>
        <servlet-name>corso.app.ApplicationConfig</servlet-name>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>ApplicationConfig</param-value>
        </init-param>
    </servlet>
</web-app>

申请

@ApplicationPath("/app")
public class ApplicationConfig extends Application {
    public Set<Class<?>> getClasses() {
        Set<Class<?>> s = new HashSet<Class<?>>();
        s.add(RecipeManager.class);
        s.add(UserManager.class);
        return s;
    }
}

我的上下文是 javaee-1.0

我不明白,因为如果我调用这个端点 http://localhost:8080/javaee-1.0/app/user/all 一切正常。下面是用户管理器的代码

@Path("/user")
@RequestScoped
public class UserManager implements UserManagerInterface {

    @Inject
    UserServiceInterface userService;

    @Override
    @Path("/all")
    @Produces(MediaType.APPLICATION_JSON)
    @GET
    public List<UserDTO> getUsers() {
        return userService.getUsers();
    }

    @Override
    @Path("/")
    @DELETE
    @Consumes(MediaType.APPLICATION_JSON)
    public void deleteUser(UserDTO userDTO) {
        userService.deleteUser(userDTO);
    }

    @Override
    @Path("/")
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public void saveUser(UserDTO userDTO) {
        userService.saveUser(userDTO);
    }

    @Override
    @GET
    @Path("/")
    @Produces(MediaType.APPLICATION_JSON)
    public List<UserDTO> getUserByIdAndName(@QueryParam("id") Long id,@QueryParam("name") String name) {
        return userService.getUserByIdAndName(id,name);
    }

    @Override
    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public List<UserDTO> getUserById(@PathParam("id") Long id) {
        return userService.getUserById(id);
    }
}

最后,我测试了 User 和 Recipe Manager 的所有其他端点,并且所有其他端点都正常工作。

我使用野蝇。 Wildfly 不记录任何内容

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)