I have created a simple java web service class like follows
public class Customer {
private String customerName;
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
I did this using Eclipse Indigo enterprise edition & I used Axis2 as soap engine. Every thing fine.I created web service successfully & deployed it on Tomcat 7 server(wsdl also ok).
Now I want create a client program which can update the name variable. I created web service client using Eclipse & it generates CustomerCallbackHandler & CustomerStub java classes automatically. But I don't know how to develop a client using that classes to update variable. Please help me....
I implemented a client like this...
import java.rmi.RemoteException;
import com.spikes.ws.CustomerWSStub.SetName;
import com.spikes.ws.CustomerWSStub.GetNameResponse;;
public class TestClient {
public static void main(String[] args) throws RemoteException{
CustomerWSStub.SetName obj = new CustomerWSStub.SetName();
obj.setName("Kenth");
CustomerWSStub.GetNameResponse res = new CustomerWSStub.GetNameResponse();
System.out.println(res.get_return());
}
}
But when I'm running the code It gives "null" as result. What is wrong?? & how can I correct that ??? please help me.....
Please list your webservice method. You have listed, I'm assuming the class you pass up.
CustomerWSStub.SetName obj = new CustomerWSStub.SetName();
obj.setName("Kenth");
You need to call the web method on the class
CustomerWSStub.SetName obj = new CustomerWSStub.SetName();
obj.setName("Kenth");
CustomerWSStub stub = new CustomerWSStub();
CustomerWSStub.GetNameResponse res = stub.GetName(obj);
Please post your actual web service method and that will be helpful.
Related
I'm trying to create a Web Service with Axis2 and Tomcat 7. Everything is working great except I don't understand the following behavior:
I've created a Web Service with 2 operations, one sets an int local variable and the other one returns it, code looks like this:
package testServer;
public class service {
public int number;
public void setNumber(int i){ this.number = i; }
public int getNumber(){ return this.number; }
}
Client side looks like this:
package testserver;
import java.rmi.RemoteException;
import testserver.ServiceStub;
import testserver.ServiceStub.*;
public class CallService {
public CallService(){};
public void call() throws RemoteException{
ServiceStub s = new ServiceStub();
ServiceStub.SetNumber params = new ServiceStub.SetNumber();
params.setI(2);
s.setNumber(params);
ServiceStub.GetNumber n = new ServiceStub.GetNumber();
ServiceStub.GetNumberResponse r = s.getNumber(n);
System.out.println("number is: " + r.get_return());
}
}
Now, I'm expecting to get a "number is: 2" but instead I'm getting a "number is: 0". Can Anyone explain that to me please?
Because at each invocation, a different instance of the class is used.
In the client, you only have a ServiceStub instance. But the server is creating a new instance of Service (check CAPITALIZATION!!) for each request you make.
This is not as bad as you may think, think that the server does not really know where the requests are from.
To get the 2, you could make the variable static, just for testing. The "real" solution would be calling your server business logic methods (EJBs, POJOs) and have them store and retrieve the value.
I need to invoke some web service methods within a java web application that I'm building.
E.g each time a user signs up, I want to call the newUser method on a WSDL url via Java. I'd need to pass on some parameters with the request.
Is there any built in Java class, or any publicly available class, which can make this easy, i.e I just supply the URL and the parameters, and it performs the request and returns the response?
If not, what is the standard way of invoking web services on WSDL in Java applications?
Run wsimport on the deployed WSDL URL , you can run it from your JDK:
wsimport -p client -keep http://localhost:8080/calculator?wsdl
This step will generates and compile some classes. Notice -keep switch, you need it to keep the generated Java source files.
Calculator.java - Service Endpoint Interface or SEI
CalculatorService - Generated Service, instantiate it
public class MyClientServiceImpl {
public static void main(String args[]){
#Override
public Integer add(int a , int b) {
CalculatorService service = new CalculatorService();
Calculator calculatorProxy = service.getCalculatorPort();
/**
* Invoke the remote method
*/
int result = calculatorProxy.add(10, 20);
System.out.println("Sum of 10+20 = "+result);
}
}
If you are using the Java EE 6 supported container then you can use it in this way ,
public class MyClientServiceImpl implements MyClientService {
#WebServiceRef(wsdlLocation = "http://localhost:8080/calculator?wsdl",
value = CalculatorService.class)
private Calculator service;
#Override
public Integer add(int a , int b) {
return service.add(a,b);
}
}
I'm new I need your help, and I hope to be helpful in future.
When I try to create a web service starting from Java class
public class AddOperation {
public int add(int a, int b){
return a+b;
}
}
with Tomcat 6 Eclipse Helios Axis2 1.6.0 (eclipse plugin) at the time of generating the client and use it get
Eclipse Plugin creation Server
Eclipse Plugin creation Client
public class TestClient {
public static void testClientOperation() throws RemoteException{
AddOperationStub aos = new AddOperationStub();
Add add = new Add();
add.setA(2);
add.setB(3);
AddResponse addResponse = aos.add(add);
int result = addResponse.get_return();
System.out.println("Result is: "+result);
}
}
Instead, expect to have
public class TestClient {
public static void testClientOperation() throws RemoteException{
AddOperationStub aos = new AddOperationStub();
int result = aos.add(2, 3);
System.out.println("Result is: "+result);
}
}
where I'm wrong, because it creates the type Add?
Thanks to all.
By default Axis2 use doc/lit/wrapped style but what you have expected here is doc/lit/bare style, you need to add following property to services.xml file to generate bare service.
<parameter name="doclitBare" locked="false"> true</parameter>
If you have further issue write to user list http://axis.apache.org/axis2/java/core/mail-lists.html
So i'm trying to get my Apache xmlrpc client/server implementation to play ball. Everything works fine except for one crucial issue:
my handler class (mapped through the properties file org.apache.xmlrpc.webserver.XmlRpcServlet.properties) reacts as it should but it's constructor is called at every method invocation. It would seem that the handler class is instantiated at each call which is bad because I have data stored in instance variables that I need to save between calls.
How do I save a reference to the instantiated handler so that I can access it's instance variables?
So, for anyone else who still wants to use XMLRPC here's how I fixed this issue:
http://xmlrpc.sourceforge.net/
far superior to apache xmlrpc, in my opinion.
This is standard behaviour of Apache XMLRPC 3.x. http://ws.apache.org/xmlrpc/handlerCreation.html:
By default, Apache XML-RPC creates a new object for processing each
request received at the server side.
However, you can emulate the behaviour of XMLRPC 2.x, where you registered handler objects instead of handler classes, using a RequestProcessorFactoryFactory. I have written a custom RequestProcessorFactoryFactory that you can use:
public class CustomHandler implements RequestProcessorFactoryFactory {
Map<Class<?>, RequestProcessorFactory> handlers =
Collections.synchronizedMap(
new HashMap<Class<?>, RequestProcessorFactory>());
#Override
public RequestProcessorFactory getRequestProcessorFactory(Class pClass)
throws XmlRpcException {
return handlers.get(pClass);
}
public void addHandler(final Object handler) {
handlers.put(handler.getClass(), new RequestProcessorFactory() {
#Override
public Object getRequestProcessor(XmlRpcRequest pRequest)
throws XmlRpcException {
return handler;
}
});
}
}
This can then be used with e.g. a XMLRPC WebServer like this
WebServer server = ...
PropertyHandlerMapping phm = new PropertyHandlerMapping();
server.getXmlRpcServer().setHandlerMapping(phm);
Custom sh = new CustomHandler();
phm.setRequestProcessorFactoryFactory(sh);
Object handler = ... /** The object you want to expose via XMLRPC */
sh.addHandler(handler);
phm.addHandler(serverName, handler.getClass());
Maybe something to do with javax.xml.rpc.session.maintain set to true?
I know this is a really old post but I managed to solve the problem with Apache's Java XML-RPC.
First, I thought this could be solved with singleton class in Java but it doesn't work and throws "illegal access exception".
These are what I have done:
public class XmlRpcServer {
private static JFrame frame = new JFrame();
private static JPanel pane = new JPanel();
public static XmlRpcServer singleton_inst = new XmlRpcServer();
public XmlRpcServer() {
// I kept the constructor empty.
}
public static void main(String[] args) throws XmlRpcException, IOException {
// In my case, I put the constructor code here.
// Then stuff for XML-RPC server
// Server Part
WebServer ws = new WebServer(8741);
PropertyHandlerMapping mapping = new PropertyHandlerMapping();
mapping.addHandler("SERVER", singleton_inst.getClass());
ws.getXmlRpcServer().setHandlerMapping(mapping);
ws.start();
////
}
// I called doTheJob() from python via XML-RPC
public String doTheJob(String s) throws XmlRpcException {
loop();
return s;
}
// It executed loop() forever
private static void loop() throws XmlRpcException {
// Actual work is here
}
But metaspace increases gradually:
I worked too much on this metaspace issue when looping forever in Java but I couldn't figure out a solution.
I've run through the Google Web Toolkit StockWatcher Tutorial using Eclipse and the Google Plugin, and I'm attempting to make some basic changes to it so I can better understand the RPC framework.
I've modified the "getStocks" method on the StockServiceImpl server-side class so that it returns an array of Stock objects instead of String objects. The application compiles perfectly, but the Google Web Toolkit is returning the following error:
"No source code is available for type com.google.gwt.sample.stockwatcher.server.Stock; did you forget to inherit a required module?"
It seems that the client-side classes can't find an implementation of the Stock object, even though the class has been imported. For reference, here is a screenshot of my package hierarchy:
I suspect that I'm missing something in web.xml, but I have no idea what it is. Can anyone point me in the right direction?
EDIT: Forgot to mention that the Stock class is persistable, so it needs to stay on the server-side.
After much trial and error, I managed to find a way to do this. It might not be the best way, but it works. Hopefully this post can save someone else a lot of time and effort.
These instructions assume that you have completed both the basic StockWatcher tutorial and the Google App Engine StockWatcher modifications.
Create a Client-Side Implementation of the Stock Class
There are a couple of things to keep in mind about GWT:
Server-side classes can import client-side classes, but not vice-versa (usually).
The client-side can't import any Google App Engine libraries (i.e. com.google.appengine.api.users.User)
Due to both items above, the client can never implement the Stock class that we created in com.google.gwt.sample.stockwatcher.server. Instead, we'll create a new client-side Stock class called StockClient.
StockClient.java:
package com.google.gwt.sample.stockwatcher.client;
import java.io.Serializable;
import java.util.Date;
public class StockClient implements Serializable {
private Long id;
private String symbol;
private Date createDate;
public StockClient() {
this.createDate = new Date();
}
public StockClient(String symbol) {
this.symbol = symbol;
this.createDate = new Date();
}
public StockClient(Long id, String symbol, Date createDate) {
this();
this.id = id;
this.symbol = symbol;
this.createDate = createDate;
}
public Long getId() {
return this.id;
}
public String getSymbol() {
return this.symbol;
}
public Date getCreateDate() {
return this.createDate;
}
public void setId(Long id) {
this.id = id;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
}
Modify Client Classes to Use StockClient[] instead of String[]
Now we make some simple modifications to the client classes so that they know that the RPC call returns StockClient[] instead of String[].
StockService.java:
package com.google.gwt.sample.stockwatcher.client;
import com.google.gwt.sample.stockwatcher.client.NotLoggedInException;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
#RemoteServiceRelativePath("stock")
public interface StockService extends RemoteService {
public Long addStock(String symbol) throws NotLoggedInException;
public void removeStock(String symbol) throws NotLoggedInException;
public StockClient[] getStocks() throws NotLoggedInException;
}
StockServiceAsync.java:
package com.google.gwt.sample.stockwatcher.client;
import com.google.gwt.sample.stockwatcher.client.StockClient;
import com.google.gwt.user.client.rpc.AsyncCallback;
public interface StockServiceAsync {
public void addStock(String symbol, AsyncCallback<Long> async);
public void removeStock(String symbol, AsyncCallback<Void> async);
public void getStocks(AsyncCallback<StockClient[]> async);
}
StockWatcher.java:
Add one import:
import com.google.gwt.sample.stockwatcher.client.StockClient;
All other code stays the same, except addStock, loadStocks, and displayStocks:
private void loadStocks() {
stockService = GWT.create(StockService.class);
stockService.getStocks(new AsyncCallback<String[]>() {
public void onFailure(Throwable error) {
handleError(error);
}
public void onSuccess(String[] symbols) {
displayStocks(symbols);
}
});
}
private void displayStocks(String[] symbols) {
for (String symbol : symbols) {
displayStock(symbol);
}
}
private void addStock() {
final String symbol = newSymbolTextBox.getText().toUpperCase().trim();
newSymbolTextBox.setFocus(true);
// Stock code must be between 1 and 10 chars that are numbers, letters,
// or dots.
if (!symbol.matches("^[0-9a-zA-Z\\.]{1,10}$")) {
Window.alert("'" + symbol + "' is not a valid symbol.");
newSymbolTextBox.selectAll();
return;
}
newSymbolTextBox.setText("");
// Don't add the stock if it's already in the table.
if (stocks.contains(symbol))
return;
addStock(new StockClient(symbol));
}
private void addStock(final StockClient stock) {
stockService.addStock(stock.getSymbol(), new AsyncCallback<Long>() {
public void onFailure(Throwable error) {
handleError(error);
}
public void onSuccess(Long id) {
stock.setId(id);
displayStock(stock.getSymbol());
}
});
}
Modify the StockServiceImpl Class to Return StockClient[]
Finally, we modify the getStocks method of the StockServiceImpl class so that it translates the server-side Stock classes into client-side StockClient classes before returning the array.
StockServiceImpl.java
import com.google.gwt.sample.stockwatcher.client.StockClient;
We need to change the addStock method slightly so that the generated ID is returned:
public Long addStock(String symbol) throws NotLoggedInException {
Stock stock = new Stock(getUser(), symbol);
checkLoggedIn();
PersistenceManager pm = getPersistenceManager();
try {
pm.makePersistent(stock);
} finally {
pm.close();
}
return stock.getId();
}
All other methods stay the same, except getStocks:
public StockClient[] getStocks() throws NotLoggedInException {
checkLoggedIn();
PersistenceManager pm = getPersistenceManager();
List<StockClient> stockclients = new ArrayList<StockClient>();
try {
Query q = pm.newQuery(Stock.class, "user == u");
q.declareParameters("com.google.appengine.api.users.User u");
q.setOrdering("createDate");
List<Stock> stocks = (List<Stock>) q.execute(getUser());
for (Stock stock : stocks)
{
stockclients.add(new StockClient(stock.getId(), stock.getSymbol(), stock.getCreateDate()));
}
} finally {
pm.close();
}
return (StockClient[]) stockclients.toArray(new StockClient[0]);
}
Summary
The code above works perfectly for me when deployed to Google App Engine, but triggers an error in Google Web Toolkit Hosted Mode:
SEVERE: [1244408678890000] javax.servlet.ServletContext log: Exception while dispatching incoming RPC call
com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract com.google.gwt.sample.stockwatcher.client.StockClient[] com.google.gwt.sample.stockwatcher.client.StockService.getStocks() throws com.google.gwt.sample.stockwatcher.client.NotLoggedInException' threw an unexpected exception: java.lang.NullPointerException: Name is null
Let me know if you encounter the same problem or not. The fact that it works in Google App Engine seems to indicate a bug in Hosted Mode.
GWT needs the .java file in addition to the .class file. Additionally, Stock needs to be in the "client" location of a GWT module.
The GWT compiler doesn't know about Stock, because it's not in a location it looks in. You can either move it to the client folder, or if it makes more sense leave it where it is and create a ModuleName.gwt.xml that references any other classes you want, and get your Main.gwt.xml file to inherit from that.
eg: DomainGwt.gwt.xml
<module>
<inherits name='com.google.gwt.user.User'/>
<source path="javapackagesabovethispackagegohere"/>
</module>
and:
<module rename-to="gwt_ui">
<inherits name="com.google.gwt.user.User"/>
<inherits name="au.com.groundhog.groundpics.DomainGwt"/>
<entry-point class="au.com.groundhog.groundpics.gwt.client.GPicsUIEntryPoint"/>
</module>
There's a better answer here: GWT Simple RPC use case problem : Code included
Basically, you can add parameters to your APPNAME.gwt.xml file so the compiler to give the compiler a path to the server-side class.
I was getting the same issue and the "mvn gwt:compile" output was not very helpful.
Instead, when I tried deploying to tomcat (via the maven tomcat plugin: mvn tomcat:deploy) I got helpful error messages.
A few things I had to fix up:
Make the object that is sent from the client to the server implement Serializable
Add an empty-arg constructor to that same object
Yes, it is sure that we need to use the Serialization for getting the server objects to the client. These modile?? file settings won't work to use the Stock class in the client side.
In your case you have only one class Stock and you can create a StockClient in client side. It is easy. But what will be the solution if anyone having more classes. Something like the properties of this class are also some other type of classes.
Example: stock.getEOD(date).getHigh();
getEOD will return another class with the given date and that class has the getHigh method.
What to do in such big cases? I don't think creating all classes implementing serialization in client side is good for that. Then we have to write code in both server and client. all classes two times.
Keying off of rustyshelf's answer above ...
In my case I needed to edit the ModuleName.gwt.xml file and add the following:
<source path='client'/>
<source path='shared'/>
I created my project with the New->Web Application Project wizard but unchecked the Generate project sample code option. I then created the shared package. Had I not unchecked that, the package would have been created for me and the xml file modified per the above.
There is a far more simple and easy solution for that. If you want to send an object of your custom designed class from server side to client side you should define this custom class in shared package.
For example for your case the you just have to carry the Stock.java class (by drag and drop) into
com.google.gwt.sample.stockwatcher.shared
package. However from your package hierarchy screenshot it seems that you had deleted this shared package. Just re-create this package and drop the Stock.java inside it and let the game begin.