java REST Web 服务 - 示例代码 - 提供 HTTP 302 响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10646825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
REST Webservice - Sample Code - Gives HTTP 302 response
提问by Indu Mudigonda
I am just trying REST webservice; Standard Sample; While I am able to create the war and load it into WSO2 AS, when I try to connect it with client I get a response of HTTP 302; I was wondering where the mistake could be; Following are my file listings.
我只是在尝试 REST 网络服务;标准样品;虽然我能够创建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
<?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>
client
客户
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();
}
}
回答by artbristol
302 means you are being redirected. Connect to the endpoint using curl --verbose, and take a look at the Location: header. It should give you a clue about what's happening.
302 表示您正在被重定向。使用 curl --verbose 连接到端点,并查看 Location: 标头。它应该为您提供有关正在发生的事情的线索。
回答by Indu Mudigonda
Ok here is the solution I finally got from a friend of mine;
好的,这是我最终从我的一个朋友那里得到的解决方案;
in web.xml change /rest --> /rest/*
在 web.xml 中更改 /rest --> /rest/*
Create the war file and upload into the server (in my case WSO2); When the server uploads the war file, it creates it with a context (in my case RSTest)
创建war文件并上传到服务器(在我的例子中是WSO2);当服务器上传 war 文件时,它会使用上下文创建它(在我的情况下为 RSTest)
Finally go to a browser type:
最后转到浏览器类型:
http://localhost:9100/RSTest/rest/hello
and you will see a successful result (in this case Hello Jersey )
你会看到一个成功的结果(在这种情况下 Hello Jersey )
-Indu
-Indu