@Retention(value=SOURCE) @Target(value=METHOD) public @interface OnReceive
Model
can be marked by this annotation to establish a
JSON
communication point.
The associated model class then gets new method to invoke a network
connection. Example follows:
When the server returns@Model(className="MyModel", properties={@Property(name = "people", type=Person.class, array=true) }) class MyModelImpl {@Model(className="Person", properties={@Property(name = "firstName", type=String.class),@Property(name = "lastName", type=String.class) }) static class PersonImpl {@ComputedPropertystatic String fullName(String firstName, String lastName) { return firstName + " " + lastName; } }@OnReceive(url = "{protocol}://your.server.com/person/{name}") static void getANewPerson(MyModel m, Person p) { alert("Adding " + p.getFullName() + '!'); m.getPeople().add(p); } // the above will generate methodgetANewPersonin classMyModel. // withprotocolandnamearguments // which asynchronously contacts the server and in case of success calls // your@OnReceivewith parsed in data@Functionstatic void requestSmith(MyModel m) { m.getANewPerson("http", "Smith"); } }
{ "firstName" : "John", "lastName" : "Smith" }
the browser will show alert message Adding John Smith!.
One can use this method to communicate with the server via WebSocket protocol since version 0.6. Read the tutorial to see how.
| Modifier and Type | Required Element and Description |
|---|---|
String |
url
The URL to connect to.
|
| Modifier and Type | Optional Element and Description |
|---|---|
Class<?> |
data
The model class to be send to the server as JSON data.
|
String |
jsonp
Support for JSONP requires
a callback from the server generated page to a function defined in the
system.
|
String |
method
The HTTP transfer method to use.
|
String |
onError
Name of a method in this class which should be called in case of
an error.
|
public abstract String url
public abstract String jsonp
jsonp attribute
one turns on the JSONP
transmission and specifies the name of the property. The property should
also be used in the url() attribute on appropriate place.public abstract Class<?> data
transport methods
(like "PUT" and "POST") require the
data to be specified.@Model annotationpublic abstract String method
"GET".
Other typical methods include "HEAD",
"DELETE", "POST", "PUT".
The last two mentioned methods require data() to be specified.
When JSONP transport is requested, the method
has to be "GET".
Since version 0.5 one can specify "WebSocket" as the communication method.
public abstract String onError
Exception
parameter. If this method is not specified, the exception is just
printed to console.Copyright © 2014 NetBeans. All Rights Reserved.