I have a web app that hosts web pages, web sockets, and I'm trying to add a REST service. It's running on Tomcat 8 and I'm using Jersey 2.11 for REST. I'm using ApplicationPath and extending Application to register my service, and pausing in the debugger tells me that getClasses is being called and my handlers returned.
But for the life of me, I can't figure out the URL for the handlers. Everything I try returns a 404, except for the static pages I have, as well as the websocket handler.
Here is my Application:
#ApplicationPath("rest")
public class RestApp extends Application {
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<>();
s.add(Test.class);
return s;
}
}
And here is my test REST handler:
#Path("test")
public class Test {
#GET
#Produces(MediaType.TEXT_PLAIN)
public String testMethod(){
return "test";
}
}
http://localhost:8080/webapp will return my index.html. But I can't find the URL for my testMethod above. http://localhost:8080/webapp/rest/test/testMethod returns a 404 error. I've tried variations omitting parts of the URL, but without success. The URL mapping as I understand it is: http://localhost:8080/<appname>/<ApplicationPath>/<Path>/<method>
Is the mapping incorrect, or am I missing something here?
Related
I am working on Quarkus application and what I want to do is to set the global path from application.properties file for all rest rest, my application is working but while calling rest request it is giving not found 404.
#ApplicationScoped
public class ABC {
#POST
#javax.ws.rs.Path("/callit")
public Uni<Response> deleteNoti()
{
//whatever logic
}
}
#ApplicationScoped
public class PAR {
#POST
#javax.ws.rs.Path("/callitPar")
public Uni<Response> addNoti()
{
//whatever logic
}
}
And in application.properties file I am configuring below properties:
quarkus.resteasy.path=/rest/*
quarkus.rest.path=/rest/*
quarkus.http.root-path=/myapp
but when I am calling rest request from front-end it is not working, my rest request should be as below:
http://localhost:8080/myapp/rest/callit
http://localhost:8080/myapp/rest/callitPar
What I want is every rest request should start with "/rest/*" and my application base URL should be "/myapp", Let me know how can we achieve it?
Try to annotate your resource classes with #Path("/") and set quarkus.resteasy.path=/rest.
This should result in your described behaviour.
quarkus.rest.path can be removed.
Problem:
I have a WebFilter which forwards valid urls to either a proxy servlet or a servlet which handles a webpage for the admin to monitor recent requests and more.
The admin servlet is suppost to forward ajax requests to a REST service (after login.jsp from the webpage rendered by controlpannel.jsp ) but apparently the rest service has a different context as the WebFilter and WebServlets ?
Question:
So is it at all possible to forward from my WebServlet to the rest helper servlet (and its resource classes) ?
More specific Information:
This is how I use forwarding:
ServletContext sc = request.getServletContext();
RequestDispatcher dispatcher = sc.getRequestDispatcher(forwardURI);
dispatcher.forward(request, response);
I tried to forward to this uri:
forwardURI = /REST/proxy_client/newer_than
My rest helper servlet:
#Stateless
#ApplicationPath("/REST")
public class RestService extends Application {
#Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> restResourceClasses = new HashSet<Class<?>>();
restResourceClasses.add(ProxyClientResource.class);
return restResourceClasses;
}
}
And this resource class:
#Path("/proxy_client")
#Stateless
public class ProxyClientResource {
#EJB
private ProxyClientBean proxyClientBean;
#GET
#Produces("application/json")
#Path("/newer_than")
public String getNumberOfEntriesNewerThanTimestamp(#QueryParam("timestamp") Expression<Timestamp> indexTimestamp,
#QueryParam("numberOfclients") Integer numberOfclients) {
List<ProxyClient> pageData = proxyClientBean.getElementsNewerThan(numberOfclients, indexTimestamp);
return convertToJSONstring(pageData);
}
Solution attempt:
I found this question about how to call a rest web service from a servlet, but they use a client and no forwarding.
EDIT:
I had a configuration problem (might still have one), so now when I try to forward to my rest helper servlet (the one extending javax.ws.rs.core.Application) I get this error:
RestServlet is currently unavailable
(in the web.xml I call the Servlet RestServlet)
when accessing the REST api directly I get:
HTTP Status 500 - Authenticator.invoke() failed
but I can't find out what this means.
Edit2:
I will try repacing the subclass of Applicaton with a config in web.xml subclassing and #ApplicationPath dont seem to work for me. Also when I try to get the rest ServletsContext I get an error that no class has been specified, which is something you do when using the web.xml config.
Edit3:
I'm deploying my application on HCP and with the underlying problem beeing that I cant even access my REST service I found this SAP discussion.
When I get my REST service working without forwarding I will report back here.
Edit4:
This actually answers the question from Edit3
I had to add jersey 1.19.1 (not 2.x because im using Java EE6 which only supports up to DWP 3.0 not 3.1 as required) to by projects libraries otherwise It would say that I didn't specify a servlet class (but when I tried to add javax.ws.rs.core.Application it would tell me this is no Servlet class even though I have seen this configuration).
My real problem was that the javax.ws.rs.core.Application from the Java ee6 container on SAP Hana Cloud Platform did not work for a unkown reason.
The solution was to download and add the jersey-bundle-1.19.1.jar to WEB-INF/lib and the projects libraries.
There is no problem at all to forward a request from a vanilla servlet to the rest service! If it does not work in your case its most likely your setup or some unexpected reason like it was in my case.
I'm getting very confused over whether you can or can't run spring boot stuff and REST endpoints in one application. At the moment I have them in separate project directories, running the springboot UI one with:
#SpringBootApplication
public class LeagueProjectUiApplication {
public static void main(String[] args) {
SpringApplication.run(LeagueProjectUiApplication.class, args);
}
}
and the REST rest endpoints with:
mvn tomcat7:run
and my jersey and tomcat stuff are declared in my pom.xml
Rest:
#Path("/university")
public class University {
#GET
#Path("/{universitycode}")
#Produces(MediaType.APPLICATION_JSON)
public Response returnSingleSummoner(
#PathParam("universitycode") String universityCode) {
}
What's the best way of running both SpringBoot and REST endpoints at the same time, or am I getting completely confused!
Thanks.
When you say REST endpoints, do you mean Jersey endpoints?
Spring Boot supports Jersey,as you can see, for example, here, so nothing theoretically should stop you for putting everything in one application as long as request paths are different.
I am new to restful service and trying to learn using jersey framework.
I am using jersey version 2.14, Tomcat 7.
I am not using web.xml file, so I am registering rest class packages using #ApplicationPath("/") annotation.
The code is as follows,
#ApplicationPath("/")
public class MyApplication extends ResourceConfig {
public MyApplication() {
System.out.println("Registering packages");
packages("com.sap.earchive.rest.service");
}
}
And rest class is having a simple test code....
#Path("/archive")
public class ArchiveService {
#Path("/test")
#GET
#Produces(MediaType.TEXT_PLAIN)
public String getIt() {
return "Got it!";
}
}
but when ever I try to call url http://localhost:8080/EArchive9/archive/test using postman I am getting 404 error.
I added a log statement "Registering packages" but i am not seeing this in tomcat logfile or console.
I think I am missing something. Could anyone please help me?
I am writing a web service like
#Path("/pathName")
public class LoginServiceComponent {
#GET
#Path("/methodPathName/{param}")
#Produces(MediaType.TEXT_HTML)
public String getVoterByVoterId( #PathParam("param") String param)
{
.................
}
}
Here my url to access web service is http://www.abc.com/pathName/methodPathName/1
Here i have 10 methods.Is there any possibility to remove class level #Path means i have only one web service class in my project.So i dont want to use class level #Param repeatedly.
Thanks in advance...
If you want to avoid the #Path on the class so your URL's don't have the "pathName" in the path, I don't think you can remove the #Path on the class entirely. But I have used the #Path class annotation of #Path("/") and was able to get just URL to be just http://www.abc.com/methodPathName/1 (if that's what you're trying to do).