I am trying to run a simple "Hello World" RESTful web service on my machine. I use Eclipse Kepler and GlassFish 4.0. I am able to deploy the service and I see it on the admin pages of GlassFish but when I try to access to it I get the following error: "HTTP Status 404 - Not Found".
Herein the code of the simple service:
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
#Path("hello")
public class HelloRest {
#SuppressWarnings("unused")
#Context
private UriInfo context;
/**
* Default constructor.
*/
public HelloRest() {
// TODO Auto-generated constructor stub
}
/**
* Retrieves representation of an instance of HelloRest
* #return an instance of String
*/
#GET
#Produces("application/xml")
public String getXml() {
// TODO return proper representation object
return "<greeting>Hello REST</greeting>";
}
/**
* PUT method for updating or creating an instance of HelloRest
* #param content representation for the resource
* #return an HTTP response with content of the updated or created resource.
*/
#PUT
#Consumes("application/xml")
public void putXml(String content) {
}
}
In order to access to the service I try the following URL: http://127.0.0.1:8080/hello-rest/hello, where hello-rest is the name of the Eclipse project and the root path suggested by the admin page of GlassFish.
Your code seems to be ok, so the problem most likely is that you have not defined the base url for your service. You need to tell JAX-RS (Jersey is the implementation in GlassFish) which url pattern it must intercept as your endpoint (base url).
One way of achieving this is using an Application Class which can be added to any package in the project (you could alternatively define the necessary in a web.xml file which I won't cover). The following is the code:
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
#ApplicationPath("your-base-url")
public class ApplicationConfig extends Application {
}
You use the #ApplicationPath annotation to define your base url.
You can then access your service at http://127.0.0.1:8080/hello-rest/your-base-url/hello
#GET
#Produces("application/xml")
#path("/getxml")
public String getXml() {
// TODO return proper representation object
return "<greeting>Hello REST</greeting>";
}
Your url should be http://localhost:8080/hello-rest/hello/getxml
You should mention the path at the method level , so the request is redirected to appropriate method.
You can use SOAPUI to test the service. Please make sure you have choose the correct method.
Method as 'GET'
End point as 'hostname:8080'
Resource as 'hello-rest/hello/getxml'
If you face the same issue. Please attach more logs / exception in tomcat console.
Related
I created a simple RESTful web service on the GlassFish server and run it according to this tutorial in the IntelliJ IDE. This runs fine based on the instruction provided. I have 2 additional questions,
a. The tutorial uses a service class provide below,
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
#Path("/helloworld")
public class HelloWorld {
#GET
#Produces("text/plain")
public String getClichedMessage() {
return "Hello World";
}
}
I can access that from the URL provided,
http://localhost:8080/AppointmentManager_war_exploded/helloworld
Afterward, I add a new class in the same directory,
#Path("/")
public class App {
#GET
#Produces("text/plain")
public String getMessage() {
return "Hello, Berlin";
}
}
I expected to see the message "Hello, Berlin" in the browser from the opening URL http://localhost:8080/AppointmentManager_war_exploded/, but, instead, I get the error provided,
HTTP Status 404 - Not Found
type Status report
messageNot Found
descriptionThe requested resource is not available.
GlassFish Server Open Source Edition 5.0
What is the issue here?
b. How do I change the part of URL AppointmentManager_war_exploded to something else, say, appointment etc? The artifact tab in the project setting is provided below,
I edited it, but, the change it not corresponded as expected.
I changed the project to maven build after the tutorial, but, the issue is not created for that. If someone interested, you can try too as it will take a minute to run.
Thank you.
First
I expected to see the message "Hello, Berlin" in the browser from the opening URL http://localhost:8080/AppointmentManager_war_exploded/, but, instead, I get the error provided
In MyApplication class that provided by tutorial you should also add your new class:
#ApplicationPath("/")
public class MyApplication extends Application{
#Override
public Set<Class<?>> getClasses() {
HashSet h = new HashSet<Class<?>>();
h.add(HelloWorld.class);
h.add(App.class); // Add your new class here
return h;
}
}
Then you will be able to see expected page on http://localhost:8080/AppointmentManager_war_exploded/
Second
How do I change the part of URL AppointmentManager_war_exploded to something else, say, appointment etc?
URL contains name of your artifact AppointmentManager_war_exploded. This artifact automatically copied to glassfish application directory. You can check glassfish\domains\domain1\applications\__internal.
Just change it just in project structure window here:
Update
Don't forget to change start URL in configuratin settings for app:
I'm executing a REST tutorial using jersey 2.26. I'm running in apache tomcat v 8.0.
I'm trying to change the response on my resource. Initially it was returning, "Got it!". Then I wanted to change it and I even tried adding a different resource with a new #PATH. Even though I changed the path annotation on my resource, changed the value of the String I'm returning and bounced the server a handfull of times, every time I navigate to this resource, it's returning "Got it!"
Why doesn't it return "Got it! This worked" since I changed the return value?
package com.demo.jersey.message.resource;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
/**
* Root resource (exposed at "myresource" path)
*/
#Path("myresource")
public class MyResource {
/**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "text/plain" media type.
*
* #return String that will be returned as a text/plain response.
*/
#GET
#Produces(MediaType.TEXT_PLAIN)
public String getIt() {
return "Got it! this worked";
}
}
I want to ask the basic question about Jersey Application.
Is it possible to add and display HTML content in Base URL? If it is possible, how can I implement it?
I use Jersey 2.X Application.
In the usual, I extends the ResourceConfig to implement and set the ApplicationPath as "/api".
Besides, I set the resource Path as "test" in Test class and define testResp() with "GET" request.
I use maven to build service.war and deploy on local tomcat, so I can access http://localhost:8080/service/api/test to get result in browser.
But now, I want to display some HTML content in API Base URL: http://localhost:8080/service/api/
For example, I will put some introduction for this service and
user can access API Base URL to read.
How can I implement it if possible? Thanks a lot!
Following is the some code example.
Jersey Application sample:
#ApplicationPath("/api")
public class WebApplication extends ResourceConfig {
public WebApplication() {
/**
* register resource, load setting, ...etc
*/
}
}
Resource sample:
#Path("test")
public class Test {
#GET
public Response testResp() {
/**
* impliment the action of http://localhost:8080/service/api/test/
*/
}
}
Create a resource class annotated with #Path("/") or #Path("") then create a resource method to handle GET requests producing HTML:
#Path("/")
public class WelcomeResource {
#GET
#Produces(MediaType.TEXT_HTML)
public Response produceHTML() {
String html = "<p>Hello, World!</p>";
return Response.ok(html).build();
}
}
The HTML content will be available at /api.
I have made a barebones hello world webservice using netbeans and jersey.
My problem is when I deploy my webservice to the server (I'm using glass fish) It takes me to the index page, but I cannot invoke the method I created in the java class.
The domain my glass fish service is using is
http://localhost:8080/HelloWorldApp/
To invoke my method (from what ive read) this is the way to do it:
http://localhost:8080/HelloWorldApp/helloworld
However this gives me a 404. Ive followed many examples but cant seem to invoke the method in my web browser.
I can however invoke the method when I click test RESTful Webservices in netbeans.
Here is how I Defined the Method:
package HelloWorldResource;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/helloworld")
public class Hellworld {
#Context
private UriInfo context;
/**
* Creates a new instance of Hellworld
*/
public Hellworld() {
}
/**
* Retrieves representation of an instance of HelloWorldResource.Hellworld
* #return an instance of java.lang.String
*/
#GET
#Produces(MediaType.TEXT_HTML)
public String getHtml() {
//TODO return proper representation object
return "<HTML>Hello</HTML>";
}
/**
* PUT method for updating or creating an instance of Hellworld
* #param content representation for the resource
*/
#PUT
#Consumes(MediaType.TEXT_HTML)
public void putHtml(String content) {
}
I looked up another tutorial and found some information. Theres a java class that's created called "ApplicationConfig.java" that has this tag: #javax.ws.rs.ApplicationPath("webresources")
so I had to invoke the method using this uri http://localhost:8080/HelloWorldApp/webresources/helloworld
Please share you web.xml and resource config implementation. If you have any custom resource config implementation, then your resource file has to be registered in the resource config implementation. For details please refer http://cloudskol.com/index.php/2015/09/22/simple-get-method-implementation-in-restful-java/
I am trying to expose some RESTfull webservices on AEM. I have followed the instructions in this blog. Below is my service class. Simple requests like /helloservice/sayhi works perfectly, but the method that take path parameter and query parameters (/withparameter/{userid} and /query?q=xyz&prod=abc) return 404 error page. Please help me with this.
I am using AEM 5.6 and Java 7
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import com.foo.bar.service.SampleResource;
#Service
#Component(metatype=true)
#Path("/helloservice")
public class SampleResourceImpl implements SampleResource{
#GET
#Path("/sayhi")
#Produces({MediaType.APPLICATION_JSON})
public String helloWorld(){
return "Hello World";
}
#GET
#Path("/getoperation")
#Produces({MediaType.TEXT_PLAIN})
public String getServiceOperation(){
return "Hello getoperation!";
}
#GET
#Path("/withparameter/{userid}")
#Produces({MediaType.TEXT_PLAIN})
public String getWithParameter(#PathParam("userid") String userid){
return "Path parameter : "+userid;
}
#GET
#Path("/query")
#Produces({MediaType.TEXT_PLAIN})
public String getURLParameters(#QueryParam("q") String q, #QueryParam("prod") String prod){
return "Query params : "+q+", "+prod;
}
}
Any help appreciated, Thanks!
There's an ongoing discussion about using JAX-RS in systems based on Apache Sling (which includes AEM) at https://issues.apache.org/jira/browse/SLING-2192 . From that discussion, https://github.com/wcm-io-caravan/caravan-jaxrs looks to me like a good solution to use JAX-RS resources in an OSGi environment. That project's integration tests run on Sling so there's a fair chance that it works on AEM.
This is wrong usage of Sling architecture.
If you want to implement some RESTful service (querying by path, etc) you need to implement specific resource provider.
There is an example. You may need to understand some basic concepts behind it but still its 10 times better in Sling's world than JAX-RS.
Cognifide has implemented alternative solution which is called Knot.X.
It allows to easly write micro services (and rest APIs). It was written for AEM, but it can be used with any CMS/CRM.
Informations, code examples and docs can be found here: http://knotx.io/
I passed the parameters as querystring, then with "UriInfo" from javax.ws.rs.core worked for me:
#GET
#Path("/data")
#Produces({MediaType.APPLICATION_JSON})
public JSONArray getData(#Context UriInfo info) throws JSONException {
String path = info.getQueryParameters().getFirst("p");
String nodename = info.getQueryParameters().getFirst("nn");
Some paths and methods of requests are blocked by default on AEM. Go to "Apache Sling Servlet/Script Resolver and Error Handler" on config to allow this /services and go to "Apache Sling Referrer Filter" to remove blocked HTTP methods.