gpt4 book ai didi

用于 RESTful Web 服务的 Java Jersey

转载 作者:行者123 更新时间:2023-12-02 05:59:35 26 4
gpt4 key购买 nike

我正在使用 Java Jersey 为我的应用程序编写 Web 服务,但在配置方面遇到了一些问题。当我按如下方式调用我的服务时,它显示:

message Method Not Allowed description The specified HTTP method is not allowed for the requested resource (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>

最佳答案

Jersey 的版本是什么?

因为......我用过这个,而且它有效!

<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

关于用于 RESTful Web 服务的 Java Jersey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22783935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com