Hi,
I am truing to build a simple REST based API.
Unfortunately, I am stack at the beginning - can't get HttpServletRequest
passed to my resource class.
here they are:
SimpleApi.java:
package org.dcache.restsample.api;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
@Path("/api")
public class SimpleApi {
@Path("/remote")
@GET
public String getRemoteAddress(@Context HttpServletRequest request) {
return request.getRemoteAddr();
}
}
App.java:
package org.dcache.restsample;
import com.sun.jersey.api.container.grizzly2.servlet.GrizzlyWebContainerFactory;
import com.sun.jersey.spi.container.servlet.ServletContainer;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.core.UriBuilder;
import org.glassfish.grizzly.http.server.HttpServer;
public class App {
public static final URI BASE_URI = UriBuilder
.fromUri("
http://localhost/")
.port(8000)
.build();
private static HttpServer initServer() throws IOException {
System.out.println("Starting grizzly... " + BASE_URI);
Map<String, String> initParams = new HashMap<String, String>();
initParams.put("com.sun.jersey.config.property.packages", "org.dcache.restsample.api");
return GrizzlyWebContainerFactory.create(BASE_URI, ServletContainer.class, initParams);
}
public static void main(String[] args) throws Exception {
HttpServer httpServer = initServer();
httpServer.start();
System.in.read();
httpServer.stop();
}
}
Probably some trivial error, which I can't catch. The error always the same:
....
Sep 13, 2013 3:46:54 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
class org.dcache.restsample.api.SimpleApi
Sep 13, 2013 3:46:54 PM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
Sep 13, 2013 3:46:54 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.17.1 02/28/2013 12:47 PM'
Sep 13, 2013 3:46:55 PM com.sun.jersey.spi.inject.Errors processErrorMessages
SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
SEVERE: Missing dependency for method public java.lang.String org.dcache.restsample.api.SimpleApi.getRemoteAddress(javax.servlet.http.HttpServletRequest) at parameter at index 0
SEVERE: Method, public java.lang.String org.dcache.restsample.api.SimpleApi.getRemoteAddress(javax.servlet.http.HttpServletRequest), annotated with GET of resource, class org.dcache.restsample.api.SimpleApi, is not recognized as valid resource method.
Exception in thread "main" com.sun.jersey.spi.inject.Errors$ErrorMessagesException
Thanks a lot,
Tigran.