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
Related
I've setup a Dynamic Web Project in Eclipse to deploy as a EAR in WildFly 15 and I've copy-pasted my custom Java classes and the library files, since I'm not using Maven for this project for simplicity sake. I've of course also copy-pasted the web contents into the folder webapp.
My problem is the following:
I'm trying to launch the web contents (function of a web server) and also have an POST request endpoint active at the same time, but when I launch the web app the welcome page loads index.html but when I try to post it via a button it throws out 404 for the URL, even though it's defined in one of the custom Java classes.
What's funny under JAX-WS Web Services it shows the necessary classes in order for it to function but it simply does not work:
Am I missing something here in order to bootstrap it?
My HelloWorldServicePublisher looks like the following:
package main.java.com.projectname.vorlagenwerk;
import javax.xml.ws.Endpoint;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
//import static org.jboss.arquillian.container.impl.MapObject.log;
public class HelloWorldServicePublisher {
public static void main(String[] args) throws SQLException, Exception {
//Settings config=null;
//config=new Settings();
//Connection conn = null;
//Connection jdbcConnection = null;
// postgres adapter
FreshDB creator = new FreshDB();
creator.createDB();
Endpoint.publish("http://localhost:8080/vorlagenwerkwebservice_child/HelloWorldServiceImpl", new HelloWorldServiceImpl());
}
}
The endpoint should return the string test for now.
I am trying to create a Web service using JAX-WS. I do have a very basic Java project with the following:
EmployeeService .java
import javax.jws.WebMethod;
import javax.jws.WebService;
#WebService
public class EmployeeService {
#WebMethod
public String getEmployee(String id) {
return "Vlad Danila";
}
}
Exporter.java
import javax.xml.ws.Endpoint;
import services.EmployeeService;
public class Exporter {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/hello",
new EmployeeService());
System.out.println("Successfull!");
}
}
Running the above will throw no error and print "Successfull!".
However, accessing http://localhost:8080/hello on browser gives This page isn’t working.
What am I missing?
I did an example with your code, and it works.. you have to add this to the browser to see
http://localhost:9999/ws/hello?wsdl
This is the url on my case. Then consume it with soap ui or another ws client.
The error you see its cause you are doing a get request on that url and not a soap request.
You don't give much context about what you are doing. JAX-WS is supposed to run in container. Do you run in container which is JEE compatible. See this tutorial, especially the last part:
https://docs.oracle.com/javaee/6/tutorial/doc/bnayn.html#gjyge
If you want something simple, I would recommend to make a spring-boot app, which will work out of the box for you. Forget about heavy JEE containers and try to run a simple spring-boot app which have integrated server inside the spring-boot app.
Here is a link to follow: https://spring.io/guides/gs/rest-service/
I am making a basic Hello World Web Service with help of some tutorials online.
I made a basic Java Project(non dynamic) in Eclipse. On running the code as Java Application and visiting the URL "http://localhost:9292/ws/hello" I receive"localhost page isn't working-ERR_EMPTY_RESPONSE" on my browser.Following is the code. Please let me know what am I doing wrong.
SayHello.java
package com.example.hello;
import javax.jws.WebMethod;
import javax.jws.WebService;
#WebService
public class SayHello {
#WebMethod
public String getHello(String name) {
return "Hello " + name;
}
}
LaunchService.java
package com.example.hello;
import javax.xml.ws.Endpoint;
public class LaunchService {
public static void main(String[] args) {
Endpoint.publish("http://localhost:9292/ws/hello", new SayHello());
}
}
#WebService and associated annotations are used for JAX-WS which are SOAP Web services. Requests to the service are made via POST so that is why your GET does not work. GET for ?WSDL is request for service descriptor.
With SOAP client it will work fine (e.g. SOAP UI)
if you want to build REST service then use JAX-RS or Restlet or something else
(2 years late to the party but I thought I might help someone :) )
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 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.