Java REST Web 服务显示 404
Java REST Web Service shows 404
我用 Jersey 创建了一个 RESTful 应用程序并部署在 Apache Tomcat。
我尝试访问该服务时收到 404。
我的web.xml
是:
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
我的代码是:
@Path("/countries")
public class SolrLogService {
@Context ServletContext context;
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Countries> getCountries()
{
List<Countries> listOfCountries = new ArrayList<Countries>();
listOfCountries=createCountryList();
return listOfCountries;
}
@GET
@Path("{id: \d+}")
@Produces(MediaType.APPLICATION_JSON)
public Countries getCountryById(@PathParam("id") int id)
{
List<Countries> listOfCountries = new ArrayList<Countries>();
listOfCountries=createCountryList();
for (Countries country: listOfCountries) {
if(country.getId()==id)
return country;
}
return null;
}
当我尝试访问以下内容时
http://localhost:8080/LogEngine/rest/countries
显示:
type Status report
message Not Found
description The requested resource is not available.
@GET
@Path("AllCountries")
@Produces(MediaType.APPLICATION_JSON)
public List<Countries> getCountries() {
List<Countries> listOfCountries = new ArrayList<Countries>();
listOfCountries=createCountryList();
return listOfCountries;
}
在返回国家列表的函数中包含路径注释,并将您的 URL 更改为如下所示
http://localhost:8080/LogEngine/rest/countries/AllCountries
@Path
注解如果只有一个@GET
则不是必须的。可能是您需要接受 JSON 的请求。因此,要么添加接受请求 header 并将其设置为 Application/JSON,要么更改代码生成 TEXT 并查看它是否有效
您还没有为 Jersey servlet 配置任何资源。大多数时候你只是希望它扫描你的资源包。要做到这一点,你可以简单地做
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
org.foo.myresources,org.bar.otherresources
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
其中 org.foo.myresources,org.bar.otherresources
是您的资源 类 所在的两个逗号分隔包。
我用 Jersey 创建了一个 RESTful 应用程序并部署在 Apache Tomcat。
我尝试访问该服务时收到 404。
我的web.xml
是:
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
我的代码是:
@Path("/countries")
public class SolrLogService {
@Context ServletContext context;
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Countries> getCountries()
{
List<Countries> listOfCountries = new ArrayList<Countries>();
listOfCountries=createCountryList();
return listOfCountries;
}
@GET
@Path("{id: \d+}")
@Produces(MediaType.APPLICATION_JSON)
public Countries getCountryById(@PathParam("id") int id)
{
List<Countries> listOfCountries = new ArrayList<Countries>();
listOfCountries=createCountryList();
for (Countries country: listOfCountries) {
if(country.getId()==id)
return country;
}
return null;
}
当我尝试访问以下内容时
http://localhost:8080/LogEngine/rest/countries
显示:
type Status report
message Not Found
description The requested resource is not available.
@GET
@Path("AllCountries")
@Produces(MediaType.APPLICATION_JSON)
public List<Countries> getCountries() {
List<Countries> listOfCountries = new ArrayList<Countries>();
listOfCountries=createCountryList();
return listOfCountries;
}
在返回国家列表的函数中包含路径注释,并将您的 URL 更改为如下所示 http://localhost:8080/LogEngine/rest/countries/AllCountries
@Path
注解如果只有一个@GET
则不是必须的。可能是您需要接受 JSON 的请求。因此,要么添加接受请求 header 并将其设置为 Application/JSON,要么更改代码生成 TEXT 并查看它是否有效
您还没有为 Jersey servlet 配置任何资源。大多数时候你只是希望它扫描你的资源包。要做到这一点,你可以简单地做
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
org.foo.myresources,org.bar.otherresources
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
其中 org.foo.myresources,org.bar.otherresources
是您的资源 类 所在的两个逗号分隔包。