I have a web service that I built with Axis2/Java. The web service will take in a list of objects and will process them. I'll use the following operation as an example.
public class AddToDatabaseService{
public void addToDatabase(String name1, String name2, String name3, ....)
{
//add names to database
}
}
I want the caller of my web service to use a URI like:
http://localhost:8080/axis2/services/addToDatabase?name1=Joe&name2=Bob&name3=Kelly&name4=...
I'm aware this is not compilable code and the idea is still there. Is this even possible with SOAP-based web services? I know this is possible to do with RESTful services because you can just take the HttpServletRequest request object and do Enumeration enumeration = request.getParameterNames() and iterate through them.
I'm looking for the equivalent of that in web services POJO's with Axis2.
A link or an sample program would be great!
Thanks.
Similar to the this question
Solution was to use a varargs parameter
Related
I'm doing an assignment where I have to use a web service using apache axis (Using eclips Mars) to make a desktop application in Java. It has to use an existing dynamic web project I already created.
Web project was to add/remove companies and employee details in a (Oracle) database in a web interface. It worked as required. But when the web service was created, It doesn't allow me to create a web client. It gives this error:
IWAB0399E Error in generating Java from WSDL:
java.io.IOException: ERROR: Missing <soap:fault> element inFault "IOException"
in operation "IOException", in binding getCompanies
Apparently, it wont allow me to return HashMaps from methods I created. (When I changed my whole project without returning Hashmaps, I can create the client) But I need to get HashMaps.
Are there any way to get HashMaps from the web service I created ???
I've refereed This question in SO. But I have no idea what's the accepted answer was saying.
EDIT:
OK. Now I know that I can't use HashMaps in web services as they can't be marshal and unmarshal. Then I found This question which I tried. But the problem still stands. (I guess I didn't used the answer mentioned above correctly.) As a beginner in this field, I actually don't get how to wrap (Or serialize) Hashmap and retrieve it back. Can someone show an example ?
You can try to wrap your HashMap in a class and create a custom adapter using it with #XmlJavaTypeAdapter to allow JAXB to make the object serialisation correctly.
public class Response {
#XmlJavaTypeAdapter(MapAdapter.class)
HashMap<Integer, Student> students;
public HashMap<Integer, Student> getStudents() {
return students;
}
public void setStudents(HashMap<Integer, Student> map) {
this.students = map;
}
}
Then just use this class as a return value of your web method.
See more:
Doc API
Example
I am developing an Android app using GAE on Eclipse.
On one of the EndPoint classes I have a method which returns a "Bla"-type object:
public Bla foo()
{
return new Bla();
}
This "Bla" object holds a "Bla2"-type object:
public class Bla {
private Bla2 bla = new Bla2();
public Bla2 getBla() {
return bla;
}
public void setBla(Bla2 bla) {
this.bla = bla;
}
}
Now, my problem is I cant access the "Bla2" class from the client side. (Even the method "getBla()" doesn't exist)
I managed to trick it by creating a second method on the EndPoint class which return a "Bla2" object:
public Bla2 foo2()
{
return new Bla2();
}
Now I can use the "Bla2" class on the client side, but the "Bla.getBla()" method still doesn't exit. Is there a right way to do it?
This isn't the 'right' way, but keep in mind that just because you are using endpoints, you don't have to stick to the endpoints way of doing things for all of your entities.
Like you, I'm using GAE/J and cloud endpoints and have an ANdroid client. It's great running Java on both the client and the server because I can share code between all my projects.
Some of my entities are communicated and shared the normal 'endpoints way', as you are doing. But for other entities I still use JSON, but just stick them in a string, send them through a generic endpoint, and deserialize them on the other side, which is easy because the entity class is in the shared code.
This allows me to send 50 different entity types through a single endpoint, and it makes it easy for me to customize the JSON serializing/deserializing for those entities.
Of course, this solution gets you in trouble if decide to add an iOS or Web (unless you use GWT) client, but maybe that isn't important to you.
(edit - added some impl. detail)
Serializing your java objects (or entities) to/from JSON is very easy, but the details depend on the JSON library you use. Endpoints can use either Jackson or GSON on the client. But for my own JSON'ing I used json.org which is built-into Android and was easy to download and add to my GAE project.
Here's a tutorial that someone just published:
http://www.survivingwithandroid.com/2013/10/android-json-tutorial-create-and-parse.html
Then I added an endpoint like this:
#ApiMethod(name = "sendData")
public void sendData( #Named("clientId") String clientId, String jsonObject )
(or something with a class that includes a List of String's so you can send multiple entities in one request.)
And put an element into your JSON which tells the server which entity the JSON should be de serialized into.
Try using #ApiResourceProperty on the field.
I'm trying to build a Java REST web service that will do some processing on a get request (eg. send get request with info, do some calculations, then send back an object with the results). Any ideas how I can set this up easily in Netbeans? I've been playing with the New->RESTful web service... feature, but can't seem to get it to return an object.
AFAIK you're supposed to return a string representation of the result. For example implementing the getXml() method:
/**
* Retrieves representation of an instance of services.GenericResource
* #return an instance of java.lang.String
*/
#GET
#Produces("application/xml")
public String getXml() {
return "<entry></entry>";
}
You could use an XML API to turn your objects into XML strings and return them.
What kind of object do you want to return...?
In java rest webservice you can return many kinds of objects like json,xml.
You can follow these tutorials for creating any kind of java rest webservice -
http://www.mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson/
This link shows example of get request which returns a json object. You can browse there tutorials for any other requirements.
I haven't tried it in Netbeans, but I have done it using Intellij tho, using maven. Just used servlets to get the requests and used GSON to convert the outgoing java object to JSON and send it out.
This is the project I did with some of my colleges.
I've just started using CXF 2.4 to expose some methods in an existing web application. One of the methods returns a complex type object - Employee which has an Address object as a property.
The employee object looks like:
public class Employee implements Serializable {
private String gid;
private String name;
private Address employeeAddress;
//...getters and setters omitted
}
The method signature in the service side interface looks like:
Employee getEmployee(#WebParam(name="gid") String gid);
On the client side I used the CXF WsdlToJava utility to generate my client side stubs from the server wsdl address and was happily using the Employee object in my client web application.
Today a co-worker pointed out to me that I shouldn't be using the webservice generated objects in my client application code. Instead I should create an Employee class specific to my client web app and copy the properties from the web service Employee object to my application Employee object to prevent the web service code ending up throughout the code base.
To me this seems like overkill, in effect I'll create an Employee class plus an Address class and copy properties back and forth between objects of those types and those of the webservices.
In my application I'm displaying the details of the Employee object on a JSP page but not doing much else with it.
So to sum up my question - when using webservice calls in CXF which return complex type objects, should you always use separate client application specific objects which copy the properties of the webservice generated objects? Is there a rule to use or does it depend on how complex the objects returned are and what the client application intends to do with them (e.g. just display them, edit them and return them, store them in a client application db.)
Thanks
Darren
I don't see any problem in using webservice generated object on the client side.
And creating own class and move the prperties back and forth is just a time killing process.
When tool is providing you the implementation feature why we need to manually do the changes.If there is a valid point then need to be figure out.
So my answear is you can work with the webservice generated Employee class for your implementation.
I was wondering how people with more experience and more complex projects get along with this "uglyness" in the REST Communication. Imagine the following Problem:
We'll need a fair amount of functionalities for one specific resource within our REST Infrastructure, in my case that's about 50+ functions that result in different querys and different responses. I tried to think of a meaningful resource-tree and assigned these to methods that will do "stuff". Afterwards, the Server Resource Class looks like this:
#Path("/thisResource")
public class SomeResource {
#GET/POST/PUT/DELETE
#Path("meaningfulPath")
public Response resourceFunction1 ( ...lots of Params) {
... logic ....
}
//
// lots of functions ...
//
#GET/POST/PUT/DELETE
#Path("meaningfulPath")
public Response resourceFunctionN ( ...lots of Params) {
... logic ....
}
}
To construct the urls my client will call, I made a little function to prevent Typos and to take better use of Constants
so my Client looks like this:
public class Client() {
public returnType function1 () {
client.resource = ResourceClass.build(Constants.Resouce, "meaningfulPath");
...
return response.getEntity(returnType);
}
}
Now the questions that bothers me is how could I link the client function and the server function better?
The only connection between these two blocks of code is the URL that will be called by the client and mapped by the server, and if even this URL is generated somewhere else, this leads to a lot of confusion.
When one of my colleagues needs to get into this code, he has a hard time figuring out which of the 50+ client functions leads to wich server function. Also it is hard to determine if there are obsolete functions in the code, etc. I guess most of you know about the problems of unclean code better than I do.
How do you deal with this? How would you keep this code clean, maintainable and georgeous?
Normally, this would be addressed by EJB or similar technologies.
Or at least by "real" web services, which would provide at least WSDL and schemas (with kind of mapping to Java interfaces, or "ports").
But REST communication is very loosely typed and loosely structured.
The only thing I can think of now, is: define a project (let's call it "Definitions") which would be referenced (hence known) by client and server. In this project you could define a class with a lot of public static final String, such as:
public static final String SOME_METHOD_NAME = "/someMethodName";
public static final String SOME_OTHER_METHOD_NAME = "/someOtherMethodName";
Note: a static final String can very well be referenced by an annotation (in that case it is considered to be constant by the compiler). So use the "constants" to annotate your #Path, such as:
#Path(Definitions.SOME_METHOD_NAME)
Same for the client:
ResourceClass.build(Constants.Resouce, Definitions.SOME_METHOD_NAME);
You are missing the idea behind REST. What you are doing is not REST but RPC over HTTP. Generally you are not supposed to construct URLs using out of band knowledge. Instead you should be following links received in the responses received from the server. Read about HATEOAS:
http://en.wikipedia.org/wiki/HATEOAS