JSON4Jersey
From APIDesign
(→Jersey Media HTML JSON) |
(→Jersey Media HTML JSON) |
||
Line 22: | Line 22: | ||
</source> | </source> | ||
- | The ([[Jersey]] and [[Grizzly]] based) server starts on port 8080. Just open your [[Chrome]] browser and point it to ''http://localhost:8080''. In a few seconds, you should see a chat application running via the [[Bck2Brwsr]] [[VM]]. The message is represented by a [https://hg.java.net/hg/html~demo/file/65cadba2cf02/chat/model/src/main/java/org/apidesign/html/chatserver/model/MessageImpl.java Message] class and this class is the same on the server as well as in the browser. It is transmitted in a [[JSON]] format, but it can also contain additional (for example validation) logic. | + | The ([[Jersey]] and [[Grizzly]] based) server starts on port 8080. Just open your [[Chrome]] browser (there are some problems with [[Firefox]] and my use of '''XHR''''s ''POST'' method) and point it to ''http://localhost:8080''. In a few seconds, you should see a chat application running via the [[Bck2Brwsr]] [[VM]]. The message is represented by a [https://hg.java.net/hg/html~demo/file/65cadba2cf02/chat/model/src/main/java/org/apidesign/html/chatserver/model/MessageImpl.java Message] class and this class is the same on the server as well as in the browser. It is transmitted in a [[JSON]] format, but it can also contain additional (for example validation) logic. |
Your code can now run on a server as well as get [[Bck2Brwsr]]! | Your code can now run on a server as well as get [[Bck2Brwsr]]! |
Revision as of 12:38, 20 May 2013
The net.java.html.json has been primarily designed to run in a browser. However almost every application in a browser tries to communicate with some server. The application logic is then split into two distinct systems - yet certain parts of the code need to run on both sides (think about validation - prevalidate on client, re-validate on server, before database is modified). Can we use the same code in browser as well as on server?
Jersey Media HTML JSON
A new module has been donated to Jersey version 2.1. Just include it on your server own application path as
<dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>html-json</artifactId> <version>2.1-SNAPSHOT</version> <scope>runtime</scope> </dependency>
and you can use classes generated by JSON4Brwsr's @Model annotation as Jersey entities. Here is a little demo for you to try:
$ hg clone https://hg.java.net/hg/html~demo $ cd html~demo/cd chat/server/ $ mvn process-classes exec:java
The (Jersey and Grizzly based) server starts on port 8080. Just open your Chrome browser (there are some problems with Firefox and my use of XHR's POST method) and point it to http://localhost:8080. In a few seconds, you should see a chat application running via the Bck2Brwsr VM. The message is represented by a Message class and this class is the same on the server as well as in the browser. It is transmitted in a JSON format, but it can also contain additional (for example validation) logic.
Your code can now run on a server as well as get Bck2Brwsr!