@Retention(value=SOURCE) @Target(value=METHOD) public @interface OnReceive
Model
can be marked by this annotation to establish a
JSON
communication point. The first argument should be the
associated Model
class. The second argument can
be another class generated by Model
annotation,
or array of such classes or (since 0.8.1 version) a
List
of such classes.
The associated model class then gets new method to invoke a network
connection asynchronously. 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 {@ComputedProperty
static String fullName(String firstName, String lastName) { return firstName + " " + lastName; } }@OnReceive
(url = "{protocol}://your.server.com/person/{name}") static void getANewPerson(MyModel m, Person p) { System.out.println("Adding " + p.getFullName() + '!'); m.getPeople().add(p); } // the above will generate methodgetANewPerson
in classMyModel
. // withprotocol
andname
arguments // which asynchronously contacts the server and in case of success calls // your@OnReceive
with parsed in data@Function
static void requestSmith(MyModel m) { m.getANewPerson("http", "Smith"); } }
{ "firstName" : "John", "lastName" : "Smith" }
the system will print a message Adding John Smith!. It is not
necessary to fully describe the server message - enumerate only the fields
in the response you are interested in. The others will be discarded. So,
if the server { "firstName" : "John", "lastName" : "Smith", "age" : 33 }
the above code will behave the same (e.g. ignore the age
value).
One can use this method to communicate with the server
via WebSocket protocol since version 0.6.
Read the tutorial to see how.
The method shall be non-private
and unless instance mode
is on also static.
Visit an on-line demo
to see REST access via OnReceive
annotation.
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[] |
headers
Specifies HTTP request headers.
|
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[] headers
OnReceive.url()
specification)
and can only be used with plain JSON(P) requests.
Headers are currently not supported by the
WebSockets protocol.
A sample follows. If you want to transmit X-Birthday header,
you can do it like this:
a method knowingTheBirth is generated in@
OnReceive
(url="http://your.server.org", headers = { "X-Birthday: {dayOfBirth}" }) static void knowingTheBirth(YourModel
model) { // handle the reply }
YourModel
class with the dayOfBirth
argument
which can be called like this:
yourModel.knowingTheBirth("10. 12. 1973");
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 OnReceive.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 OnReceive.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 © 2018 NetBeans. All rights reserved.