Java RESTWebService-示例代码-给出HTTP 302响应
我只是在尝试RESTWebService;标准样品; 虽然我能够创建war并将其加载到WSO2中,但当我尝试将其与客户端连接时,我得到了HTTP 302的响应;我想知道错误在哪里; 以下是我的文件列表Java RESTWebService-示例代码-给出HTTP 302响应,java,web-services,http-status-code-302,restful-url,restful-architecture,Java,Web Services,Http Status Code 302,Restful Url,Restful Architecture,我只是在尝试RESTWebService;标准样品; 虽然我能够创建war并将其加载到WSO2中,但当我尝试将其与客户端连接时,我得到了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,
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
302表示您正在被重定向。使用curl--verbose连接到端点,并查看位置:header。它应该会给你一个关于发生了什么的线索。好的,这是我从一个朋友那里得到的解决方案 在web.xml中 更改/rest-->/rest/* 创建war文件并上传到服务器(在我的例子中是WSO2); 当服务器上传war文件时,它会创建一个上下文(在我的例子中是RSTest) 最后转到浏览器类型:
http://localhost:9100/RSTest/rest/hello
您将看到一个成功的结果(在本例中为Hello Jersey)
-工业尝试
getBaseURI
和http://localhost:9100/
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();
}
}
http://localhost:9100/RSTest/rest/hello