REST Web 项目出现 404 错误
404 error at REST web project
我正在使用这个名为 CLAWS-Dicionario
的测试项目,我正在尝试 运行 在 Glassfish 服务器(运行完美)上进行它。我只有 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!";
}
}
并在 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>
我还将以下球衣库添加到我的构建路径中:
asm-debug-all-5.0.3.jar
javax.ws.rs-api-2.0.1.jar
jersey-server.jar
org.osgi.core-4.2.0
所以我运行这个项目,感觉一切都很好。但是当我尝试访问这个 URL:
http://localhost:8080/CLAWS-Dicionario/helloworld
它 returns 一个 404 错误。我确定主机配置在 8080 端口(URL localhost:8080
工作正常)。那么问题是什么?
编辑:服务器开始出现另一个问题,这将我带到了 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/">
修复CLAWS_-_Dicionário
部分。这是我项目的真实名称吗?为了让服务器正常工作,我不得不删除重音符号,并且在 运行 启动项目时我发现没有更多的控制台响应......即使我尝试 URL http://localhost:8080/CLAWS_-_Dicionario/helloworld
,错误依然存在,所以...只是发现了一些重要的东西要指出。
根据您使用的 Servlet 版本,您需要:
对于 Servlet 2.x 实现:
- jersey-container-servlet-core.jar
对于 Servlet 3.x 实现:
- 球衣容器-servlet.jar
org.glassfish.jersey.servlet.ServletContainer球衣未打包-server.jar
根据 api 文档 Class ServletContainer
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. The initialization parameter
"jersey.config.server.provider.packages" MUST be set to provide one or
more package names. Each package name MUST be separated by ';'.
参数 com.sun.jersey.config.property.packages
在版本 2.x 中被替换为 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>
在 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/">
允许您从以下位置访问您的应用程序:
http://localhost:8080/CLAWS-Dicionario/helloworld
我正在使用这个名为 CLAWS-Dicionario
的测试项目,我正在尝试 运行 在 Glassfish 服务器(运行完美)上进行它。我只有 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!";
}
}
并在 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>
我还将以下球衣库添加到我的构建路径中:
asm-debug-all-5.0.3.jar
javax.ws.rs-api-2.0.1.jar
jersey-server.jar
org.osgi.core-4.2.0
所以我运行这个项目,感觉一切都很好。但是当我尝试访问这个 URL:
http://localhost:8080/CLAWS-Dicionario/helloworld
它 returns 一个 404 错误。我确定主机配置在 8080 端口(URL localhost:8080
工作正常)。那么问题是什么?
编辑:服务器开始出现另一个问题,这将我带到了 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/">
修复CLAWS_-_Dicionário
部分。这是我项目的真实名称吗?为了让服务器正常工作,我不得不删除重音符号,并且在 运行 启动项目时我发现没有更多的控制台响应......即使我尝试 URL http://localhost:8080/CLAWS_-_Dicionario/helloworld
,错误依然存在,所以...只是发现了一些重要的东西要指出。
根据您使用的 Servlet 版本,您需要:
对于 Servlet 2.x 实现:
- jersey-container-servlet-core.jar
对于 Servlet 3.x 实现:
- 球衣容器-servlet.jar
org.glassfish.jersey.servlet.ServletContainer球衣未打包-server.jar
根据 api 文档 Class ServletContainer
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. The initialization parameter "jersey.config.server.provider.packages" MUST be set to provide one or more package names. Each package name MUST be separated by ';'.
参数 com.sun.jersey.config.property.packages
在版本 2.x 中被替换为 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>
在 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/">
允许您从以下位置访问您的应用程序:
http://localhost:8080/CLAWS-Dicionario/helloworld