JAX-WS : class not found - java

I am not well versed with Java. Here is the webservice, I am trying to implement - a basic example and I am facing compilation error.
I am not sure what am I missing here.
Here is the code.
package com.joshis1.jaxws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
#WebService
#SOAPBinding(style = Style.DOCUMENT)
public interface IwebServiceInterface {
#WebMethod String sayHello(String name);
}
Next, implementing the interface
package com.joshis1.jaxws;
import javax.jws.WebService;
#WebService(endpointInterface = "com.joshis1.jaxws")
public class webServiceImpl implements IwebServiceInterface {
#Override
public String sayHello(String name)
{
return "Hello Shreyas " + name;
}
}
Next, the main class to publish the endpoint
package com.joshis1.publisher;
import javax.xml.ws.Endpoint;
import com.joshis1.jaxws.*;
public class WebServicePublisher {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8888/webservice/helloworld", new webServiceImpl());
}
}
Next, very basic question - Do I need to install a web server here?

You are pointing your endpointInterface to your package:
#WebService(endpointInterface = "com.joshis1.jaxws")
It needs to reference your interface:
#WebService(endpointInterface = "com.joshis1.jaxws.IwebServiceInterface")
It is very important to look on what the error is saying
class:com.joshis1.jaxws could not be found

Related

How to test RESTful application?

I have an application example with a service:
RestApp.java
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
#ApplicationPath("/webapi")
public class RestApp extends Application {
#Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<>();
classes.add(MessageService.class);
return classes;
}
}
MessageService.java
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.net.URI;
import java.util.List;
#Stateless
#Path("/messages")
public class MessageService {
#Inject
private MessagesManager messagesManager;
#GET
#Path("all")
#Produces({MediaType.APPLICATION_JSON})
public List<Message> getMessages() {
return messagesManager.getMessages();
}
}
and the service depends on the singleton MessagesManager.java:
import javax.ejb.*;
import javax.inject.Singleton;
#Singleton
#Startup
#ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
public class MessagesManager implements Serializable {
private List<Message> messages = new ArrayList<>();
#Lock(LockType.READ)
public List<Message> getMessages() {
messages.add(new Message(1, "message text"));
return messages;
}
}
and this app works fine. But during the test occurs error of injection:
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=MessagesManager,parent=MessageService,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1232089028)
Test code is:
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;
import static org.junit.Assert.assertEquals;
public class RestAppTest extends JerseyTest {
#Override
protected Application configure() {
return new ResourceConfig(MessageService.class);
}
#Test
public void testGet() {
final Response response = target("messages/all").request().get();
assertEquals(200, response.getStatus());
}
}
Why it happens and how to fix it?
The class MessagesManager is missing in an application context. Add the class to configure method like this:
return new ResourceConfig(MessageService.class, MessagesManager.class);
You need couple of things
1> Well formed JSON structure for your REST API
2> Some kind of REST client such as advanced REST client for chrome, Mozilla etc which can be used as a plugin. POSTMAN is also a useful tool

Java web service not giving error message

I am doing a simple Hello World example of java webservices, which is of document style from this link, he told that at step Number 3 , we will get an error message "Wrapper class com.mkyong.ws.jaxws.GetHelloWorldAsString is not found.
Have you run APT to generate them?".
But with out using wsgen, I am able to run my application with out any exception and able to see the output at client end. I am unable to find the reason , why didn't I get the error message?
Here is my code:
HelloWorld.java :
package com.XXX.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
#WebService
#SOAPBinding(style = Style.DOCUMENT,use=Use.LITERAL)
public interface HelloWorld {
#WebMethod String getHelloWorldAsString(String name);
}
HelloWorldImpl.java:
package com.XXX.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
#WebService(endpointInterface = "com.XXX.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
#Override
public String getHelloWorldAsString(String name) {
// TODO Auto-generated method stub
return "Hello"+name;
}
}
Publisher.java
package com.XXX.endOP;
import javax.xml.ws.Endpoint;
import com.XXX.ws.HelloWorldImpl;
public class EndPublisher {
/**
* #param args
*/
public static void main(String[] args) {
Endpoint.publish("http://localhost:9999/ws/hello", new HelloWorldImpl());
System.out.println("Started");
}
}
After so much search found the following line
Using javac with JAX-WS annotation processor will generates the
portable artifacts used in JAX-WS services.
from this link
https://jax-ws.java.net/nonav/2.2.6/docs/ch04.html

Minecraft Bukkit Events

I'm trying to get into bukkit programming for minecraft, but for some reason I'm stuck with events. Here's my code:
Main class file:
package com.plugin1;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginDescriptionFile;
//import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import net.md_5.bungee.api.ChatColor;
public class Plugin extends JavaPlugin {
public int songStage;
public static Plugin plugin;
public void OnEnable () {
PluginDescriptionFile pluginDesc = getDescription();
Logger logger = getLogger();
plugin = this;
registerEvents(this, new BlockBreak());
logger.info(pluginDesc.getName() + " is enabled! (V. " + pluginDesc.getVersion() + ")");
}
public void OnDisable () {
PluginDescriptionFile pluginDesc = getDescription();
Logger logger = Logger.getLogger("Plugin");
plugin = null;
logger.info(pluginDesc.getName() + " is disabled! (V. " + pluginDesc.getVersion() + ")");
}
public static void registerEvents(org.bukkit.plugin.Plugin plugin, Listener... listeners) {
for (Listener listener : listeners) {
Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
}
}
public static Plugin getPlugin() {
return plugin;
}
}
Event class file:
package com.plugin1;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
public class BlockBreak implements Listener {
#EventHandler(priority = EventPriority.HIGH)
public void OnBlockBreak (BlockBreakEvent e) {
Player p = e.getPlayer();
p.sendMessage("Block broken.");
}
}
Basically, this returns no errors. I've gone through console and there's nothing. When I break a block, literally nothing happens!
I've tried a few of things: I've gone through it, tried multiple video tutorials and tried a text tutorial on the minecraft forums but still nothing. I also contacted a server owner who codes bukkit plugins, but he couldn't fix this...
If there's anyone who can help me with this, PLEASE LET ME KNOW!!!!
Thanks in advance!
Here is a code example to start the server in a process:
package me.Nightfighter001.GlobalSystem.Listener;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import me.Nightfighter001.GlobalSystem.Main.main;
public class Join implements Listener {
public Join(main main) {
plugin = main;
plugin.getServer().getPluginManager().registerEvents(this, main);
}
#EventHandler
public void onPlayerJoin(PlayerJoinEvent ev) {
ev.setJoinMessage("");
}
main plugin = main.getPlugin();
}
I think you aren't registering the Listeners in the right way...
Try this code and tell me if it works... I'm really wanting to help you
First of all don't use "Plugin" as the name for your Main Class... Use "Main" instead.
Enable:
public class Main extends JavaPlugin {
public void onEnable() {
Bukkit.getPluginManger().registerEvents(new Join(this),this);
}
}
Listener:
public class Join implements Listener {
private Main plugin;
public Join(Main plugin) {
this.plugin = plugin;
}
#EventHandler
public void onPlayerJoin(PlayerJoinEvent ev) {
ev.setJoinMessage("Just another test");
}
}
Hope it works...
I've tested your code and it really doesn't work. I think your Eventregistration isn't working. For my plugins I use this in the mainClass:
package me.Nightfighter001.GlobalSystem.Main;
import org.bukkit.Bukkit;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import me.Nightfighter001.GlobalSystem.Listener.Join;
public class main extends JavaPlugin {
public static main getPlugin() {
return plugin;
}
private static main plugin;
#Override
public void onEnable() {
plugin = this;
new Join(this);
ConsoleCommandSender console = Bukkit.getConsoleSender();
console.sendMessage(new StringBuilder("\247c[\2476GlobalSystem\247c] \247bVersion \247c")
.append(getDescription().getVersion()).append(" \247bdes Plugins wurde aktiviert!").toString());
console.sendMessage(
"\247c[\2476GlobalSystem\247c] \247bDieses Plugin darf nur benutzt werden, wenn der Entwickler \247cNightfighter001 \247bes erlaubt!");
return;
}
#Override
public void onDisable() {
ConsoleCommandSender console = Bukkit.getConsoleSender();
console.sendMessage(new StringBuilder("\247c[\2476GlobalSystem\247c] \247bVersion \2474")
.append(getDescription().getVersion()).append(" \247bdes Plugins wurde deaktiviert!").toString());
}
}
And in the EventClass:
package me.Nightfighter001.GlobalSystem.Listener;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import me.Nightfighter001.GlobalSystem.Main.main;
public class Join implements Listener {
public Join(main main) {
plugin = main;
plugin.getServer().getPluginManager().registerEvents(this, main);
}
#EventHandler
public void onPlayerJoin(PlayerJoinEvent ev) {
ev.setJoinMessage("");
}
main plugin = main.getPlugin();
}
As you can see in my example I use the PlayerJoinEvent, but it also works with the BlockBreakEvent. I hope this helps :) And sorry for my bad English ;D
Your code will work if you don't capitalize the names of the onEnable (and onDisable) methods. onEnable and OnEnable are two different methods since java is case sensitive, and since you're trying to override specific methods in the JavaPlugin super class, you'll need to spell them the exact same way.
Common convention is, as far as I know, that you start your methods with lowercase letters anyway though. The #Override annotation is very useful in catching these kinds of bugs, because it lets the compiler know that you mean to override an existing method, and if that method doesn't exist (for example if you misspelled the name or added different parameters), it will alert you (it also lets anyone reading the code immediately know you're overriding an existing method or implementing an interface).

JAX-WS Endpoint not working

I'm writing a (very) small SOAP web service using Glassfish. The code I have is below:
Milliseconds.java
package com.suture.self.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;
#WebService
#SOAPBinding(style = Style.DOCUMENT, use = Use.LITERAL)
public interface Milliseconds {
#WebMethod
String currentMilliseconds();
}
MillisecondsImpl.java
package com.suture.self.ws;
import javax.jws.WebService;
#WebService(endpointInterface = "com.suture.self.ws.Milliseconds")
public class MillisecondsImpl implements Milliseconds {
#Override
public String currentMilliseconds() {
return String.valueOf(System.currentTimeMillis());
}
}
MillisecondsEndpoint.java
package com.suture.self.ws;
import javax.xml.ws.Endpoint;
public class MillisecondsEndpoint {
public static void main(String[] args) {
Endpoint.publish("http://sutureself.com:9292/ws/milli", new MillisecondsImpl());
}
}
When I run this on my Glassfish server (through Eclipse), the admin console shows me the usual working ?wsdl and ?Tester endpoints, but not the one I have created. Hitting that url (http://sutureself.com:9292/ws/milli) in a browser also returns an "Unable to connect" error.
If I select "Launch", then I am shown two links (one for each http port in Glassfish), but these return a 404.
If I just try to hit the "Context Root" path, that also doesn't work. I need a point of entry and I just cannot find it.
What am I missing?
Please note! All the sutureself.com's in the above code are actually localhost, SO doesn't like you posting URL's with localhost evidently.
Any more information as to how my setup is configured will happily be added.
I also did an example in SOAP WS some time before but I have one doubt, why are you using Glassfish Server because you are deploying your WS by main method which will bind the address with your Service. I think for that no server is required.
Just follow these steps with eclipse for testing the WS-
Note: Please shutdown your Glassfish Server
1-Create a new java project(not dynamic web project)
2-Create a HelloWorld class in hello package
package hello;
import javax.jws.WebMethod;
import javax.jws.WebService;
#WebService
public class HelloWorld{
#WebMethod
public String method(String name)
{
return "hello " +name;
}
}
you don't need to make interface explicitly.
3:Now create a Publisher class to publish the WebService in hello package
package hello;
import javax.xml.ws.Endpoint;
public class Publisher {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Endpoint.publish("http://LH.com:9292/ws/milli", new HelloWorld());
}
}
Now you have bound your WS with http://LH.com:9292/ws/milli. Check it by http://LH.com:9292/ws/milli?wsdl or http://LH.com:9292/ws/milli?test

Removing annotations and adding xml in developing JAX-WS webservice

I am a new bie to the world of webservices , I have one query as I was developing the JAX-WS the below web service both producer and client but I was using the annotaions could you please advise me how to develop the same program without use of annotations that is using XML ..itself..
Create A Web Service Endpoint Interface
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
//Service Endpoint Interface
#WebService
#SOAPBinding(style = Style.RPC)
public interface HelloWorld{
#WebMethod String getHelloWorldAsString(String name);
}
Create A Web Service Endpoint Implementation
import javax.jws.WebService;
//Service Implementation
#WebService(endpointInterface = "com.mkyong.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld{
#Override
public String getHelloWorldAsString(String name) {
return "Hello World JAX-WS " + name;
}
}
Create A Endpoint Publisher
import javax.xml.ws.Endpoint;
import com.mkyong.ws.HelloWorldImpl;
//Endpoint publisher
public class HelloWorldPublisher{
public static void main(String[] args) {
Endpoint.publish("http://localhost:9999/ws/hello", new HelloWorldImpl());
}
}
Java Web Service Client Via Wsimport Tool
wsimport -keep http://localhost:9999/ws/hello?wsdl
It will generate necessary client files, which is depends on the provided wsdl file. In this case, it will generate one interface and one service implementation file.
finally the main class using the generated stub classes..
package com.mkyong.client;
import com.mkyong.ws.HelloWorld;
import com.mkyong.ws.HelloWorldImplService;
public class HelloWorldClient{
public static void main(String[] args) {
HelloWorldImplService helloService = new HelloWorldImplService();
HelloWorld hello = helloService.getHelloWorldImplPort();
System.out.println(hello.getHelloWorldAsString("mkyong"));
}
}
I came across this question while having the same issue...
finally found the so needed explanation here: http://jonas.ow2.org/JONAS_5_1_1/doc/doc-en/pdf/jaxws_developer_guide.pdf
look for : Overriding annotations

Categories