java - REST Web 服务 - 示例代码 - 提供 HTTP 302 响应

标签 java web-services http-status-code-302 restful-url restful-architecture

我只是在尝试 REST Web 服务;标准 sample ; 虽然我能够创建 war 并将其加载到 WSO2 AS 中,但当我尝试将其与客户端连接时,我收到 HTTP 302 的响应;我想知道错误可能出在哪里; 以下是我的文件列表。

package com.indu.rs.test;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

// POJO, no interface no extends

// The class registers its methods for the HTTP GET request using the @GET annotation. 
// Using the @Produces annotation, it defines that it can deliver several MIME types,
// text, XML and HTML. 

// The browser requests per default the HTML MIME type.

//Sets the path to base URL + /hello
@Path("/hello")
public class Hello {

    // This method is called if TEXT_PLAIN is request
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayPlainTextHello() {
        return "Hello Jersey";
    }

    // This method is called if XML is request
    @GET
    @Produces(MediaType.TEXT_XML)
    public String sayXMLHello() {
        return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
    }

    // This method is called if HTML is request
    @GET
    @Produces(MediaType.TEXT_HTML)
    public String sayHtmlHello() {
        return "<html> " + "<title>" + "Hello Jersey" + "</title>"
                + "<body><h1>" + "Hello Jersey" + "</h1></body>" + "</html> ";
    }

} // class

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>com.indu.rs.test</display-name>

  <servlet>
    <servlet-name>Jersey1</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.indu.rs.test</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>Jersey1</servlet-name>
    <url-pattern>/rest</url-pattern>
  </servlet-mapping>
</web-app>

客户端

package com.indu.rs.test.client;

import java.net.URI;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class RsTest {
    public static void main(String[] args) {
        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        WebResource service = client.resource(getBaseURI());

        // Fluent interfaces
        System.out.println(service.path("rest").path("hello").accept(
                MediaType.TEXT_PLAIN).get(ClientResponse.class).toString());

        // Get plain text
        System.out.println(service.path("rest").path("hello").accept(
                MediaType.TEXT_PLAIN).get(String.class));

        // Get XML
        System.out.println(service.path("rest").path("hello").accept(
                MediaType.TEXT_XML).get(String.class));

        // The HTML
        System.out.println(service.path("rest").path("hello").accept(
                MediaType.TEXT_HTML).get(String.class));


    }

    private static URI getBaseURI() {
        return UriBuilder.fromUri(
                "http://localhost:9100/com.indu.rs.test").build();
    }

}

最佳答案

302 表示您正在被重定向。使用curl --verbose 连接到端点,并查看Location: header 。它应该可以让您了解正在发生的事情。

关于java - REST Web 服务 - 示例代码 - 提供 HTTP 302 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10646825/

相关文章:

web-services - REST 身份验证和 HMAC/私钥(我什么时候设置?)

php - 如何在 Wordpress 中使用 XML-RPC 获取帖子?

c# - 如何强制 C# Web 服务对象属性中的最大字符串长度?

http - Firebug 不显示 302 响应

java - 如何返回 DataSnapshot 值作为方法的结果?

java - IntelliJ 建议用 for each 循环替换 while 循环。为什么?

java - 如何使用 JSON 动态创建 POJO?

post - Flutter - 处理POST请求中的状态码302

用于跟踪图像 url 的 HTTP 302、303 或 307

java - 连接一组对象,在java中按值分组