Removing annotations and adding xml in developing JAX-WS webservice - java

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

Related

JAX-WS : class not found

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

How to let user to decide which implementation to use for interface in Spring?

I'm developing an SDK, which will be used to create additional applications for batch processing. There is core-a-api module, which holds interface Client
public interface Client {
void send();
}
and core-a-impl which holds couple implementations for Client interface - HttpClient and TcpClient.
Also, there is one more core module core-b-impl, which uses a particular instance of Client interface.
public class SendingTasklet implements Tasklet {
#Autowired
private Client client
public void process() {
client.send();
}
}
What instance should be created (HttpClient or SftpClient) should be decided by the user, who creates an application using SDK. He also needs to have an ability to create its own implementation for Client and use it in SendingTasklet. A user from core dependencies can see only interfaces from -api modules. For dependency injection, I'm using Spring. All beans for particular modules are created in each module separately. The user created beans are created in user's configuration class
#Configuration
public class UsersApplicationConf {
#Bean
public Client client {
return new UsersClient();
}
}
The issue is, that somehow without exposing -impl module details for user application, he should be able to decide what Client implementation can be used from the core provided implementations or he should be able to pass one of its own.
The first thought was to use qualifiers when injecting into SendingTasklet, but then you need to create a separate instance variable for each implementation in SendingTasklet and this is not very good because if there would be more implementations for Client interface it would be required to change SendingTasklet as well. And also the problem, that user should somehow decide wich implementation to use persists.
What I did, I exposed core-a-impl for client's application. So in his configuration, he can decide what instance to create for Client interface.
#Configuration
public class UsersApplicationConf {
#Bean
public Client client {
return new HttpClient();
}
}
But this is not very smart as well and I'm thinking is there any other way how to solve this issue?
You can use strategy or factory pattern as mentioned here but personally I would go with JSR 330 that you can find an example here , below code block for spring example:
package spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static spring.Spring.Platform;
#Configuration
#ComponentScan
public class Spring {
public static void main(String[] args) {
new AnnotationConfigApplicationContext(Spring.class);
}
#Autowired
#Platform(Platform.OperatingSystems.ANDROID)
private MarketPlace android;
#Autowired
#Platform(Platform.OperatingSystems.IOS)
private MarketPlace ios;
#PostConstruct
public void qualifyTheTweets() {
System.out.println("ios:" + this.ios);
System.out.println("android:" + this.android);
}
// the type has to be public!
#Target({ElementType.FIELD,
ElementType.METHOD,
ElementType.TYPE,
ElementType.PARAMETER})
#Retention(RetentionPolicy.RUNTIME)
#Qualifier
public static #interface Platform {
OperatingSystems value();
public static enum OperatingSystems {
IOS,
ANDROID
}
}
}
interface MarketPlace {
}
#Component
#Platform(Platform.OperatingSystems.IOS)
class AppleMarketPlace implements MarketPlace {
#Override
public String toString() {
return "apple";
}
}
#Component
#Platform(Platform.OperatingSystems.ANDROID)
class GoogleMarketPlace implements MarketPlace {
#Override
public String toString() {
return "android";
}
}
Edit: I didnt test the code but I have used javax.inject.Qualifier
with CDI if this code doesnt work let me know I will update with
correct combination and imports

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

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

EJB with SOAP and XML

I need to make application but I dont know where to start...
What do I need to do is to make stateless EJB that gets XML file trough SOAP, and parse that XML file.
I tried to make bean, but i relly dont know what to do first and how (I'm new in this area):
package ServiceX;
public class ServiceBean implements Service {
public ServiceBean() {}
public String GetXML(String xml) {
return xml;
}
}
package ServiceX;
import java.rmi.RemoteException;
import java.rmi.Remote;
public interface Service extends Remote{
public String GetXML(String xml) throws RemoteException;
}

Categories