-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an example of Web Service with JSON input and output. To test, …
…use postman.
- Loading branch information
Showing
3 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/pt/ieeta/dicoogle/plugin/demo/dicooglepluginsample/JsonWebService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
|
||
package pt.ieeta.dicoogle.plugin.demo.dicooglepluginsample; | ||
|
||
import org.json.JSONException; | ||
import org.restlet.data.MediaType; | ||
import org.restlet.ext.json.JsonRepresentation; | ||
import org.restlet.representation.Representation; | ||
import org.restlet.representation.StringRepresentation; | ||
import org.restlet.resource.Get; | ||
import org.restlet.resource.Post; | ||
import org.restlet.resource.ServerResource; | ||
|
||
/** | ||
* | ||
* @author Luís A. Bastião Silva - <[email protected]> | ||
*/ | ||
public class JsonWebService extends ServerResource { | ||
|
||
|
||
@Get | ||
public Representation testJson(){ | ||
StringRepresentation sr = new StringRepresentation("{\"name\":\"rsi\"}"); | ||
|
||
sr.setMediaType(MediaType.APPLICATION_JSON); | ||
|
||
return sr; | ||
} | ||
|
||
|
||
@Post("json") | ||
public JsonRepresentation testJson(JsonRepresentation userJson){ | ||
|
||
org.json.JSONObject userObj = null; | ||
try { | ||
userObj = userJson.getJsonObject(); | ||
System.out.println(userObj.getInt("id")); | ||
System.out.println(userObj.getString("name")); | ||
//return userObj.toString(); | ||
} catch (JSONException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
|
||
JsonRepresentation sr = new JsonRepresentation("{\"name\":\"rsi\"}"); | ||
|
||
sr.setMediaType(MediaType.APPLICATION_JSON); | ||
|
||
return sr; | ||
} | ||
|
||
// You can handle all crud operations. Please, read the restlet documentation | ||
|
||
|
||
|
||
@Override | ||
public String toString(){return "jsonDemo";} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters