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/
Related
I am developing a company-interal Quarkus service. In this service, I need to call another internal service asynchronously using a customized media type.
As I am following this official document and wrote up something like this:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
#Path("/api")
#RegisterRestClient
public interface InternalService {
String CUSTOM_TYPE = "application/custom";
#GET
#Path("/hello/{id}")
#Produces({CUSTOM_TYPE})
Uni<EntityClass> getHello(#PathParam("id") long id);
}
The EntityClass is actually protobuf message in my case.
In the example (https://quarkus.io/guides/rest-client), looks like one can directly inject the bean:
#Inject
#RestClient
CountriesService countriesService;
I have a few questions:
does resteasy already provide an implementation for common media types?
How can I implement InternalService interface for my custom media type?
Yes. Please check here:
https://docs.jboss.org/resteasy/docs/4.6.0.Final/userguide/html_single/index.html#Content_Marshalling_Providers. Quarkus adds supports for some additional ones.
Just create your own Provider to handle the custom Media type and register it:
https://docs.jboss.org/resteasy/docs/4.6.0.Final/userguide/html_single/index.html#Content_Marshalling_with__Provider_classes
https://download.eclipse.org/microprofile/microprofile-rest-client-2.0/microprofile-rest-client-spec-2.0.html#_provider_declaration
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 have a web service running on tomcat 8.5, however whenever I try to update the version, the new methods do not map properly, and 404 as a result.
For example, if a prior version had a method such as host/ContextPath/HelloWorld/, that'd run completely fine, no issues there.
However if I were to add a new method, host/ContextPath/AnotherMethod/, this method would 404.
It's not a case of the newer versions aren't being run on Azure, as you can look at the deployment history and see that it's pulling the latest versions from github, and apparently deploys them successfully.
I am unsure of how to progress from here, as what I have should be working, eg the new versions are on the server, server is running and healthy etc, yet it isn't.
Additionally, by viewing what files are on the server through site\wwwroot[project files], I can actually see that the most up to date versions of the files are on the server.
However, the methods are not being exposed, so I am at a loss as to what is happening.
For example, consider the AdministrationService, which you can see the code for below:
package services;
import controllers.DatabaseController;
import controllers.EmailController;
import controllers.ProgrammeController;
import entities.Programme;
import java.io.InputStream;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonException;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import static javax.ws.rs.HttpMethod.POST;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import utilities.SQLQuery;
#Path("/Admin")
public class AdministrationService {
ProgrammeController pc = new ProgrammeController();
/**
* A simple method for testing if our service is online.
* -> GET /API/Admin/Health
* If true is returned, we will have configured our service properly.
* #return
*/
#GET
#Path("/Health")
public Boolean getHealthy(){
return true;
}
#GET
#Path("/TestEmail")
public void testEmail(#QueryParam("target")String target){
EmailController ec = EmailController.getInstance();
String messageBody = "Hello world";
String recipient = target;
String messageSubject = "Test email";
ec.sendEmail(recipient, messageBody, messageSubject);
}
}
The /Health method is an old method. It runs just fine if I access it through
myazurehost.net/ContextPath/Admin/Health
However if I attempt to do the same with
myazurehost.net/ContextPath/Admin/TestEmail?target=testemail#gmail.com
I receive a 404 error. It's not a case of the method being incorrectly called, as the error is a 404, not a 400 bad request, 405 wrong type etc. As far as the server is concerned, the method does not exist and it's baffling.
I've tried server reboots, reinitialising the project, to no avail. Any help is greatly appreciated.
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.
I created a simple web service. Well now that's done, I have to complicate things: connect to a MySQL database and communicate with it via the web service!
I can not find on the internet how to make this connection to my web service (I know very well do so when there is no web service and it is a simple Java application). But the problem that in web service I don't have a main, just methods.
That's a part of my code:
package impression;
import java.sql.*;
import java.util.Date;
import javax.jws.Oneway;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
#WebService(serviceName = "impression")
public class impression {
#WebMethod(operationName = "EnvoiMessage")
public String messageReception(#WebParam(name = "msg") String msg) {
msg="Demande recu!";
return msg;
}
/**
* Web service operation
*/
#WebMethod(operationName = "affichageDemande")
#Oneway
public void affichageDemande() {
//here i want to display the table created im my database
}
}
I will be grateful if you could help me.
I suggest you read this thread. EJB3: How to inject DataSource in EJB3 during Junit testing as raw POJO
That is what you need. An injected datasource, then you can do as you do in "regular java with a main method" :-).
Good luck