JAX-WS web service returning java bean
how can i create a webservice that returns a javabean?
@WebMethod
public Person query(@WebParam(name ="id") String id){
Person person=new Person();
person.setId(id);
person.setFirstName("Lisa");
person.setLastName("Cruz");
return person;
}
will JAX-WS generate the WSDL and XSD Schema file? do i need to edit or create a Schema file or is there something i need to set in my project.
btw, my IDE is NETBEANS5.5 and my application server is Sun Java System Application Server Platform edition 9.0.
please help me with this, i'm really new to this technology.
thanks in advance.
# 1
I wait for the response too :)I hope someone answered quickly...
# 2
You do not have to do anything the schema will be automatically generated for you. What you have below should work just fine.
# 3
The question now is :
Using document literal wrapped (Currently i use only jax-rpc and jboss)
- How do you write a client ? (to implement distant objects)
We have the webservice which returns an object "Person" with his name (String), childs (Person[ ]) etc. Can the client retrieve how to implement a "Person" and implement it whil receiving the response ? I think it's with jaxb but i need an example to see what it should look like.
Do you know if there is a tutorial somwhere cauz the one of java is a bit difficult for me
Message was edited by:
ydriutti
# 4
A new Person class will automatically be generated for you on the client. Please look at the supplychain sample included with JAX-WS.
# 5
hi guys.. i try the above code..and when i try to test the webservice in the sun java system application server..i got this error message:
Exceptions details : java.lang.reflect.InvocationTargetException
when i check the log files it shows this error:
Error in encoding SOAP Message Error in encoding SOAP Message at com.sun.xml.ws.encoding.soap.server.SOAPXMLEncoder.toSOAPMessage(SOAPXMLEncoder.java
Caused by: javax.xml.bind.MarshalException
- with linked exception:
[javax.xml.bind.JAXBException: sciencephportal.result nor any of its super class is known to this context]
at com.sun.xml.ws.encoding.jaxb.JAXBBridgeInfo.serialize(JAXBBridgeInfo.java:90)
at com.sun.xml.ws.encoding.soap.SOAPEncoder.writeJAXBBridgeInfo(SOAPEncoder.java:254)
at com.sun.xml.ws.encoding.soap.SOAPEncoder.writeBody(SOAPEncoder.java:571)
at com.sun.xml.ws.encoding.soap.server.SOAPXMLEncoder.toSOAPMessage(SOAPXMLEncoder.java:97)
... 30 more
result is the Javabean is in the package sciencephportal where the webservice also belong.
what should i do?
# 6
Hi all,
I think i've found a good example. Try to read that :
http://java.sun.com/developer/technicalArticles/WebServices/high_performance/
and to download the example (WSTest)
https://wstest.dev.java.net/
I think you should use glassfish to run the test, but i m not sure for the moment.
# 7
What does your method signature look like and what is the contents of the method on the server?
# 8
I got the Same problem and only today i tried web services
Is it right to do this way.
If so Please See the thng i've done.
Otherwise Please Suggest alternatives
I created Two Projects
1. Web Service
2. Web Client.
A Bean (It is in Web service Project)
--
public class Results
{
private int sum;
private int difference;
private int multi;
private float division;
/** Creates a new instance of Results */
public Results ()
{
}
public Results (int sum,int difference,int multi,float division)
{
this.difference=difference;this.division=division;
this.multi=multi;
this.sum=sum;
}
}
A Service Method
-
public class myService
{
// It works Fine in the client
@WebMethod
public int addTwoNumbers (@WebParam(name = "var1") int var1, @WebParam(name = "var2") int var2)
{
// TODO implement operation
return var1+var2;
}
// How do Get this Stuff Work at the Client
@WebMethod
public Results getResults (@WebParam(name = "var1") int var1, @WebParam(name = "var2") int var2)
{
Results aResult=new Results((var1+var2),(var1-var2),(var1*var2),(var1/var2));
return aResult;
}
}
--
Now, How do I get this working in the Client.
In the Ide Get get Error (Using Netbeans5.5 -Tomcat).
# 9
OK, the problem is that Results is not a bean. To be a valid bean it needs to have a empty default constructor which yours has plus it must have a getter and a setter for each property. Try the following.
public class Results
{
private int sum;
private int difference;
private int multi;
private float division;
/** Creates a new instance of Results */
public Results () { }
public Results (int sum,int difference,int multi,float division) {
this.difference=difference;this.division=division;
this.multi=multi;
this.sum=sum;
}
public int getSum() { return sum; }
public void setSum(Int sum) { this.sum = sum; }
public int getDifference() { return difference; }
public void setDifference(Int difference) { this.difference = difference; }
public int getMulti() { return multi; }
public void setMulti(Int multi) { this.multi = multi; }
public division getDivision() { return division}
public void setdivision(float division) { this.division= division;}
}
# 10
I have implemented a JAX-WS service, using annotations only, as per the examples. I let Netbeans generate the stub and proxy classes.
I return a (bean) class from a method. However on the client side, a new class is generated, in the client's package. I want to use the same class on the client side - how can I do this? - Is there some annotation I can use?
For instance:
Bean Class:
package a.b.c.d;
class aa {.....}
Service class:
package e.f.g.h;
class bb{
@WebMethod public a.b.c.d.aa myMethod().....
}
Client:
package i.j.k.l;
class cc{
.....
aa result = port.myMethod();
}
The class aa in the client will be in the i.j.k.l package, even though the a.b.c.d package is available.
# 11
There is no automatic way to have JAX-WS use those classes for you. You should be able to edit the generated service class and replace the SEI it generated on the getXXX methods with the original SEI class.
# 12
I am having the same exact problem as shalomiko. I am new to JAX-WS and web services in general and didnt really understand the answer.
What is the SEI and what does that stand for?
I have a service called "search". I see a generated class called Search. Does this sound like the generated service class that you refer to?
I am also using netbeans 5.5. When I try to edit any of the classes that are generated and rebuild the project all of my changes are gone and the original auto generated classes are back. How do I get netbeans to use my changes and not run wsimport?
# 13
SEI stands for service endpoint interface.I am not sure how you get NBs to not regenerate the classes. You might need to post that question to a NBs forum or mailing list. How are you changing the generated classes?
# 14
Thanks for the clarification. After some more investigation I dont think I should have been modifying the classes at all. My bean classes were missing some things and that led to the error I was seeing.
# 15
Do any of us succeffuly created a webservice using netbeans that returns a java bean using netbeans 5.5.1 and Sun Sun Java System App Server 9.x?
I was able to create Webservices that returns arryalist and string types etc. When I tried with javabeans, i was getting couple of issues . please advise.
# 16
tell us what the "issues" are and maybe someone can help you...
# 17
Web Service not getting deployed with Java Object as paramenter
My issue is about passing a Java Object to web service operation as parameter.
When I code the webservice operation to accept java class object as parameter, the webservice project is not getting deployed (It complies). I Use Netbeans 5.5 and Sun Java System Application Server 9.
I get the message Problem encountered during annotation processing in Application server console. If I change, the paramenter to ArrayList or string it gets deployed.
Please find the webservice code with annotation and Transfer object . Hope that some body had experinced the same before. Please advice.
import to.TestTO;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@Stateless()
@WebService()
public class TestTOWS {
/**
* Web service operation
*/
@WebMethod
public String addTestToByObject(@WebParam(name = "testTo") TestTO testTo) {
// TODO implement operation
System.out.println("Inside addTestToByObject");
return testTo.getName();
}
}
Here is the Java object. It is in package 'to'
package to;
public class TestTO {
/** Creates a new instance of TestTO */
public TestTO() {
}
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Here is the Log
Problem encountered during annotation processing;
see stacktrace below for more information.
java.lang.NullPointerException
at com.sun.tools.ws.processor.modeler.annotation.WebServiceAP.isSubtype(WebServiceAP.java:418)
at com.sun.tools.ws.processor.modeler.annotation.WebServiceAP.isRemote(WebServiceAP.java:413)
at com.sun.tools.ws.processor.modeler.annotation.WebServiceVisitor.isLegalType(WebServiceVisitor.java:817)
at com.sun.tools.ws.processor.modeler.annotation.WebServiceVisitor.isLegalParameter(WebServiceVisitor.java:710)
at com.sun.tools.ws.processor.modeler.annotation.WebServiceVisitor.isLegalMethod(WebServiceVisitor.java:669)
at com.sun.tools.ws.processor.modeler.annotation.WebServiceVisitor.methodsAreLegal(WebServiceVisitor.java:624)
at com.sun.tools.ws.processor.modeler.annotation.WebServiceVisitor.isLegalImplementation(WebServiceVisitor.java:542)
at com.sun.tools.ws.processor.modeler.annotation.WebServiceVisitor.shouldProcessWebService(WebServiceVisitor.java:352)
at com.sun.tools.ws.processor.modeler.annotation.WebServiceVisitor.visitClassDeclaration(WebServiceVisitor.java:145)
at com.sun.tools.apt.mirror.declaration.ClassDeclarationImpl.accept(ClassDeclarationImpl.java:95)
at com.sun.tools.ws.processor.modeler.annotation.WebServiceAP.buildModel(WebServiceAP.java:347)
at com.sun.tools.ws.processor.modeler.annotation.WebServiceAP.process(WebServiceAP.java:232)
at com.sun.mirror.apt.AnnotationProcessors$CompositeAnnotationProcessor.process(AnnotationProcessors.java:60)
at com.sun.tools.apt.comp.Apt.main(Apt.java:454)
at com.sun.tools.apt.main.JavaCompiler.compile(JavaCompiler.java:448)
at com.sun.tools.apt.main.Main.compile(Main.java:1075)
at com.sun.tools.apt.main.Main.compile(Main.java:938)
at com.sun.tools.apt.Main.processing(Main.java:95)
at com.sun.tools.apt.Main.process(Main.java:85)
at com.sun.tools.apt.Main.process(Main.java:67)
at com.sun.tools.ws.wscompile.CompileTool.buildModel(CompileTool.java:605)
at com.sun.tools.ws.wscompile.CompileTool.run(CompileTool.java:538)
at com.sun.tools.ws.util.ToolBase.run(ToolBase.java:56)
at com.sun.tools.ws.util.WSToolsObjectFactoryImpl.wsgen(WSToolsObjectFactoryImpl.java:44)
at com.sun.enterprise.webservice.WsUtil.runWsGen(WsUtil.java:1820)
at com.sun.enterprise.webservice.WsUtil.genWSInfo(WsUtil.java:2089)
at com.sun.enterprise.deployment.backend.ModuleDeployer.loadDescriptors(ModuleDeployer.java:396)
at com.sun.enterprise.deployment.backend.EjbModuleDeployer.deploy(EjbModuleDeployer.java:138)
at com.sun.enterprise.deployment.backend.ModuleDeployer.doRequestFinish(ModuleDeployer.java:160)
at com.sun.enterprise.deployment.phasing.J2EECPhase.runPhase(J2EECPhase.java:169)
at com.sun.enterprise.deployment.phasing.DeploymentPhase.executePhase(DeploymentPhase.java:95)
at com.sun.enterprise.deployment.phasing.PEDeploymentService.executePhases(PEDeploymentService.java:871)
at com.sun.enterprise.deployment.phasing.PEDeploymentService.deploy(PEDeploymentService.java:266)
at com.sun.enterprise.deployment.phasing.PEDeploymentService.deploy(PEDeploymentService.java:739)
at com.sun.enterprise.management.deploy.DeployThread.deploy(DeployThread.java:174)
at com.sun.enterprise.management.deploy.DeployThread.run(DeployThread.java:210)
error: compilation failed, errors should have been reported
wsgen successful
wsgen successful
Message was edited by:
ahamedkabir_SCR
# 18
I got rid of the annotation issue. I am using netbeans 5.5.1. When i tried to creatre the webservice from a web project, it worked fine for me. (initially i tried to create webservice from within ejb project ). The problem was with Annotation in value objects.