问题
So I'm trying to set up Jetty for a school project, using Jersey on the server side and Gradle to build and run it. I have my web.xml and a test that I'm trying to hit with curl. Here's what I got:
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>Project</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<welcome-file-list>
<welcome-file>../index.html</welcome-file>
</welcome-file-list>
<init-param>
<param-name>Project</param-name>
<param-value>main.java.package</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Reservations</servlet-name>
<url-pattern>api/*</url-pattern>
</servlet-mapping>
Web.java
package main.java.package;
import javax.ws.rs.Path;
Import javax.ws.rx.GET;
@Path("api/")
public class Web {
@GET
@Path("test/")
public String test() {
return "it works!";
}
}
When I curl http://localhost:8080/project/api/test
I just get a 503. So I'm assuming it's something with my web.xml
but I just can't figure out what.
回答1:
Your servlet mapping is incorrect - there is no servlet declared with the name Reservations
anywhere.
<servlet-mapping>
<!-- <servlet-name>Reservations</servlet-name> -->
<servlet-name>Project</servlet-name>
<url-pattern>api/*</url-pattern>
</servlet-mapping>
Then pls read the user-guide for jaxrs resources
The resource path would match ./project/api/api/test
and not ./project/api/test
.
So here are some fixes:
<servlet-mapping>
<servlet-name>Project</servlet-name>
<url-pattern>/api/*</url-pattern> <!-- ApplicationPath -->
</servlet-mapping>
@Path("/test") // resource path
public class Test { // changed from class Web, 'cause resource is ./test
@GET // [GET] ./project/api/test
// @Path("test/") - path is already given by class path
public String test() {
return "it works!";
}
@GET // [GET] ./project/api/test/foo
@Path("/foo")
public String testFoo() {
return "it works for foo!";
}
}
Hope this was helpfull somehow:)
回答2:
Add @javax.ws.rs.GET
to your test()
method.
来源:https://stackoverflow.com/questions/25837852/jetty-web-xml-503