Created
June 12, 2017 13:14
-
-
Save nitincoded/b1f29b8f7fee8f1cddd2079226b26ff1 to your computer and use it in GitHub Desktop.
Gson and Spark demo
This file contains hidden or 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
import com.google.gson.Gson; | |
import com.katkam.entity.Organization; | |
import com.katkam.entity.Part; | |
/** | |
* Created by Developer on 6/12/17. | |
*/ | |
public class Initial { | |
public static void main(String args[]) { | |
Gson gson = new Gson(); | |
// final String abcd = gson.toJson("abcd"); | |
// System.out.println(abcd); | |
// gson.fromJson("abcd", String.class); | |
Part p = new Part(); | |
p.partCode = "A001"; | |
p.setDescription("Part 1 A"); | |
p.org = new Organization("XYZ", "Some Corporation"); | |
System.out.println( gson.toJson(p) ); | |
// Part q = gson.fromJson("{\"partCode\":\"A001\",\"description\":\"Part 1 A\"}", Part.class); | |
Part q = gson.fromJson("{\"partCode\":\"A001\",\"description\":\"Part 1 A\",\"org\":{\"orgCode\":\"XYZ\",\"description\":\"Some Corporation\"}}\n", Part.class); | |
System.out.println(q.partCode + " : " + q.getDescription() + " : " + q.org.description); | |
} | |
} |
This file contains hidden or 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
package com.katkam.entity; | |
/** | |
* Created by Developer on 6/12/17. | |
*/ | |
public class Organization { | |
public Organization() {} | |
public Organization(String code, String desc) { orgCode = code; description = desc; } | |
public String orgCode; | |
public String description; | |
} |
This file contains hidden or 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
package com.katkam.entity; | |
/** | |
* Created by Developer on 6/12/17. | |
*/ | |
public class Part { | |
public String partCode; | |
private String description; | |
public String getDescription() { return description; } | |
public void setDescription(String a) { description = a; } | |
public Organization org; | |
} |
This file contains hidden or 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
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.katkam</groupId> | |
<artifactId>borgchat</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<dependencies> | |
<dependency> | |
<groupId>com.google.code.gson</groupId> | |
<artifactId>gson</artifactId> | |
<version>2.8.0</version> | |
</dependency> | |
<dependency> | |
<groupId>com.sparkjava</groupId> | |
<artifactId>spark-core</artifactId> | |
<version>2.6.0</version> | |
</dependency> | |
<!-- | |
<dependency> | |
<groupId>com.microsoft.sqlserver</groupId> | |
<artifactId>mssql-jdbc</artifactId> | |
<version>6.1.0.jre8</version> | |
</dependency> | |
--> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<configuration> | |
<source>1.8</source> | |
<target>1.8</target> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains hidden or 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
import com.google.gson.Gson; | |
import com.katkam.entity.Part; | |
import static spark.Spark.port; | |
import static spark.Spark.get; | |
/** | |
* Created by Developer on 6/12/17. | |
*/ | |
public class WebServer { | |
public static void main(String args[]) { | |
port(1947); | |
Gson gson = new Gson(); | |
//Sample URL: http://localhost:1947/ | |
get("/", (request, response) -> "Hello"); | |
//Sample URL: http://localhost:1947/now | |
get("/now", (request, response) -> { | |
response.header("Content-Type", "application/json"); | |
return gson.toJson(new java.util.Date()); | |
}); | |
//Sample URL: http://localhost:1947/part?code=50501 | |
get ("/part", (request, response) -> { | |
Part p = new Part(); | |
p.partCode = "P-"+request.queryParams("code"); | |
p.setDescription("Description comes here"); | |
return gson.toJson(p); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use the path "/part/:partno" with request.params(":partno")