normally figure stuff out but I've been stuck on this one for a couple days now.. building up a simple jetty server. the server runs, and localhost:8080 pulls up the right index.html file. but I'm trying to run a java back-end using @GET and @PATH annotations, but none of the paths I try seem to work. say I have the following:
@Resource
@Path("hello")
public class AjaxCalls{
@PATH("sayHi")
@GET
public String test(){
return "Hi"; //breakpoint set here to see if we hit the method
}
}
I would think that an ajax call to the url: localhost:8080/hello/sayHi would at least hit the breakpoint, but no luck. Anyone take this approach before? I'm sure it's some small detail...
One explanation for why you are not hitting the Jersey @Get
method is because you are using the wrong URL. Here is an example of a URL which should work assuming you also have the following setup:
http://localhost:8080/your-project-WAR/rest/hello/sayHi
This URL would hit the @Get
method test()
if the name of your WAR is your-project-WAR.war
, and you had a web.xml
looking something like the following:
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>Your REST service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Your REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
The Tomcat server consumes the your-project-WAR
portion of the URL, and the web.xml
file consumes the /rest
potion before it forwards the rest to the Jersey framework. As it stands now, the URL you are using is likely to be problematical because it appears to be missing so many things. I hope that this answer can set you on the right path quickly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.