简体   繁体   English

用于RESTful Web服务的Java Jersey

[英]Java Jersey for RESTful web services

I am using Java Jersey to write web services for my app and am having some problems with the cofiguration. 我正在使用Java Jersey为我的应用程序编写Web服务,并且在配置方面遇到了一些问题。 When I call my service as given below, it says: 当我拨打下面给出的服务时,它说:

message Method Not Allowed description The specified HTTP method is not allowed for the requested resource (Method Not Allowed). message方法不允许说明请求的资源不允许使用指定的HTTP方法(Method Not Allowed)。

Sample request: 样品申请:

localhost:8080/MovieManager/rest/movies?movie_count=2&country_code=USA

Here are my Service source file and web.xml file: 这是我的服务源文件和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: 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>

What's the version of Jersey ? 泽西的版本是什么?

Because ... I used this, and it works ! 因为...我用过它,它有效!

<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>

Try also 试试吧

 @Path("/test")

And use the following url : 并使用以下网址:

 localhost:8080/MovieManager/rest/test/movies?movie_count=2&country_code=USA

it is strange how I managed to fix this problem, but here is the solution: 我设法解决这个问题很奇怪,但这是解决方案:

Instead of using a: 而不是使用:

 @Path("/")

When I replaced it with a: 当我用一个替换它:

@Path("search_movie/")

and called the API with the updated URI, it works. 并使用更新的URI调用API,它可以工作。

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

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