简体   繁体   English

REST Web项目出现404错误

[英]404 error at REST web project

I'm with this test project named CLAWS-Dicionario , and I'm trying to run it on Glassfish server (which is working flawlessly). 我正在使用这个名为CLAWS-Dicionario测试项目,并且我正在尝试在Glassfish服务器上运行它(该服务器工作正常)。 All I have is that class: 我所拥有的只是那个班级:

package com.k19.restful.resources;

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

@Path("/helloworld")
public class HelloWorldResource {

    @GET 
    @Produces("text/plain") 
    public String showHelloWorld() { 
        return "Olá mundo!"; 
    }
}

And this add at web.xml in order to incorporate Jersey to it 并将其添加到web.xml中以将Jersey合并到其中

<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>com.k19.restful.resources</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

I've also added the following jersey libraries to my build-path: 我还向我的构建路径添加了以下jersey库:

asm-debug-all-5.0.3.jar
javax.ws.rs-api-2.0.1.jar
jersey-server.jar
org.osgi.core-4.2.0

So I run the project, and everything feels fine. 因此,我运行了该项目,一切正常。 But when I try to access this URL: 但是,当我尝试访问此URL时:

http://localhost:8080/CLAWS-Dicionario/helloworld

It returns a 404 error. 它返回404错误。 I'm sure the host is congured at 8080 port (the URL localhost:8080 works just fine). 我确定主机在8080端口配置(URL localhost:8080正常工作)。 So what is the problem? 那是什么问题呢?

EDIT: The server began to present another problems, which took me to that line of the domain.xml file: 编辑:服务器开始出现另一个问题,这把我带到domain.xml文件的那一行:

 <application context-root="/CLAWS_-_Dicionário" object-type="user" name="CLAWS-Dicionario" directory-deployed="true" location="${com.sun.aas.instanceRootURI}/eclipseApps/CLAWS-Dicionario/">

Repare the CLAWS_-_Dicionário part. 重复CLAWS_-_Dicionário部分。 Would this be the real name of my project? 这是我项目的真实名称吗? I had to remove the accent in order the server to work, and I'm finding no more console response when running the project...and even if I try the URL http://localhost:8080/CLAWS_-_Dicionario/helloworld , the error is still there,so...just found it something important to point. 为了使服务器正常工作,我不得不删除该重音符号,并且在运行项目时找不到更多的控制台响应...即使我尝试使用URL http://localhost:8080/CLAWS_-_Dicionario/helloworld ,错误仍然存​​在,所以...只是发现了一些重要的要点。

Depending on which Servlet version you use, you need: 根据您使用的Servlet版本,您需要:

For Servlet 2.x implementation: 对于Servlet 2.x实现:

  • jersey-container-servlet-core.jar jersey-container-servlet-core.jar

For Servlet 3.x implementation: 对于Servlet 3.x实现:

  • jersey-container-servlet.jar jersey-container-servlet.jar

org.glassfish.jersey.servlet.ServletContainer is not packaged in jersey-server.jar org.glassfish.jersey.servlet.ServletContainer未包装在jersey-server.jar中

According to the api doc for Class ServletContainer 根据ServletContainer类的api文档

If the initialization parameter is not present and a initialization parameter "jersey.config.server.provider.packages" is present (see ServerProperties.PROVIDER_PACKAGES) a new instance of ResourceConfig with this configuration is created. 如果不存在初始化参数,并且存在初始化参数“ jersey.config.server.provider.packages”(请参阅​​ServerProperties.PROVIDER_PACKAGES),则会创建具有此配置的ResourceConfig的新实例。 The initialization parameter "jersey.config.server.provider.packages" MUST be set to provide one or more package names. 初始化参数“ jersey.config.server.provider.packages”必须设置为提供一个或多个软件包名称。 Each package name MUST be separated by ';'. 每个软件包名称必须用';'分隔。

The parameter com.sun.jersey.config.property.packages have been replaced by jersey.config.server.provider.packages in version 2.x. 在版本2.x中,参数com.sun.jersey.config.property.packages已替换为jersey.config.server.provider.packages

<init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>com.k19.restful.resources</param-value>
</init-param>

In domain.xml , you should have the context-root representing the url path from where the application is accessible: domain.xml ,您应该具有context-root表示可从其中访问应用程序的URL路径:

<application context-root="/CLAWS-Dicionario" object-type="user" name="CLAWS-Dicionario" directory-deployed="true" location="${com.sun.aas.instanceRootURI}/eclipseApps/CLAWS-Dicionario/">

letting you access your application from: 让您从以下位置访问应用程序:

 http://localhost:8080/CLAWS-Dicionario/helloworld

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

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