我正在使用Java Jersey为我的应用程序编写Web服务,并且在配置方面遇到了一些问题。当我拨打下面给出的服务时,它说:
消息方法不允许 description对于请求的资源,不允许使用指定的HTTP方法(Method Not Allowed)。
样品申请:
localhost:8080/MovieManager/rest/movies?movie_count=2&country_code=USA
以下是我的服务源文件和web.xml文件:
package com.recommendation.movies;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
@Path("/")
public class MovieData {
@GET
@Path("movies")
@Produces(MediaType.TEXT_PLAIN)
public String getMovies (@QueryParam("movie_count") int movieCount, @QueryParam("country_code") String countryCode) {
String moviesJson = null; // This has the JSON list of all movies returned by the external service
// DO SOMETHING...
// returned the customized json string...
return moviesJson;
}
}
的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Movie Manager</display-name>
<servlet>
<servlet-name>Get Top Box Office Movie Listing</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.recommendation.movies</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Get Top Box Office Movie Listing</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
答案 0 :(得分:2)
泽西岛的版本是什么?
因为...我用过它,它有效!
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.test.application.sinister</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
尝试
@Path("/test")
并使用以下网址:
localhost:8080/MovieManager/rest/test/movies?movie_count=2&country_code=USA
答案 1 :(得分:0)
我设法解决这个问题很奇怪,但这是解决方案:
而不是使用:
@Path("/")
当我用:
替换它时@Path("search_movie/")
并使用更新后的URI调用API,它可以正常工作。