Reputation: 3367
I started to try out JavaFX2.2 making requests to a RESTful web service. To create my webservice I followed this Netbeans guide: http://netbeans.org/kb/docs/websvc/rest.html (based on my database).
The entities, config and service package were generated succesfully and when I test the service by right-clicking my project and choosing 'Test RESTful Web Service...' it all seems to work (getting XML response)
Now I try to create a JavaFX2 application that does requests to this webservice. For that I use a Jersey Client (also generated by Netbeans 7.2) based on my DAPFacadeREST service. When I hit a button in my application, following gets executed:
NewJerseyClient njc = new NewJerseyClient();
String s = njc.find_XML(String.class, "1");
System.out.println("Output from Server .... \n");
System.out.println(s);
This gives me following response from the server:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<dap>
<adres>Street 1</adres>
<btw></btw>
<contactPersoon></contactPersoon>
<dapid>1</dapid>
<email>[email protected]</email>
<gemeente>RandomCity</gemeente>
<land>België</land>
<naam>Paul The Man</naam>
<postcode>9999</postcode>
<tel>+32999 88 77 66</tel>
</dap>
This info is correct, but now I'm stuck. I want this to be converted to a JavaFX2 object, preferably something like this:
public class DAP {
private SimpleIntegerProperty DAPID = new SimpleIntegerProperty();
private SimpleStringProperty naam = new SimpleStringProperty("");
private SimpleStringProperty contactPersoon = new SimpleStringProperty("");
private SimpleStringProperty adres = new SimpleStringProperty("");
private SimpleStringProperty postcode = new SimpleStringProperty("");
private SimpleStringProperty gemeente = new SimpleStringProperty("");
private SimpleStringProperty land = new SimpleStringProperty("");
private SimpleStringProperty telefoonnummer = new SimpleStringProperty("");
private SimpleStringProperty btw = new SimpleStringProperty("");
private SimpleStringProperty email = new SimpleStringProperty("");
//constructor, getters & setters below
Is this even possible? I mean, with SOAP you can use proxy classes, but there SimpleIntegerProperty aren't known at the server side (JavaEE 6). That's why I'm going with REST. Is this going to be the same problem as with SOAP? Because at the server side (at the RESTful web service), the generated entities are looking like the following:
public class DAP implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "DAPID")
private Integer dapid;
@Basic(optional = false)
@NotNull
@Lob
@Size(min = 1, max = 65535)
@Column(name = "Naam")
private String naam;
@Basic(optional = false)
@NotNull
@Lob
@Size(min = 1, max = 65535)
//etc
Those the field 'naam' is just a normal String, where I want it to be a SimpleStringProperty at my client side.
I've did some reading already on JAXB but I don't get the picture what I should create at client side and what should I create on server side. If I'm not mistaking, I have to make a xsd file somewhere? But again, will this support a SimpleStringProperty as type?
Sorry for the long posts but I hope someone can help me out... Thanks in advance.
Upvotes: 1
Views: 411
Reputation: 149027
You can create an XmlAdapter
to handle SimpleStringProperty
. Since JAXB (JSR-222) is the standard binding layer for both JAX-WS (SOAP) and JAX-RS (RESTful) web services you could use this approach for both. The XmlAdapter
would look something like the following
public class SimpleStringPropertyAdatper extends XmlAdapter<String, SimpleStringProperty> {
@Override
public SimpleStringProperty unmarshal(String v) throws Exception {
return new SimpleStringProperty(v);
}
@Override
public String marshal(SimpleStringProperty v) throws Exception {
if(null == v) {
return v;
}
return v.getString(); // Or whatever the correct method is
}
}
The @XmlJavaTypeAdapter
annotation is used to configure the XmlAdapter
you can do this at the package info using a package-info
class to specify that the adapters should be used for all instances of the specified classes.
@XmlJavaTypeAdapters({
@XmlJavaTypeAdapter(value=SimpleStringPropertyAdapter.class, type=String.class),
@XmlJavaTypeAdapter(value=SimpleIntegerPropertyAdapter.class, type=Integer.class),
})
package com.example;
import javax.xml.bind.annotation.adapters.*;
For More Information
Upvotes: 1