Last active
August 29, 2015 14:02
-
-
Save up1/45e647d79a75b44f5a9e to your computer and use it in GitHub Desktop.
Demo :: Develop RESTful web service with Spark
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
exception(IllegalArgumentException.class, (e, req, res) -> { | |
res.status(400); | |
res.body(toJson(new ResponseError(e))); | |
}); |
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
exception(IllegalArgumentException.class, new ExceptionHandler() { | |
@Override | |
public void handle(Exception exception, Request request, Response response) { | |
response.status(400); | |
response.body(JsonUtil.toJson(new ResponseError(exception))); | |
} | |
}); |
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
import static spark.Spark.*; | |
public class HelloWorld { | |
public static void main(String[] args) { | |
get("/hello", (req, res) -> "Hello World"); | |
} | |
} |
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
public class HelloWorld { | |
public static void main(String[] args) { | |
get("/hello", new Route() { | |
@Override | |
public Object handle(Request arg0, Response arg1) { | |
return "Hello World"; | |
} | |
}); | |
} | |
} |
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
public class Main { | |
public static void main(String[] args) { | |
new ProductController(new ProductService()); | |
} | |
} |
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
public class Product { | |
private String productId; | |
private String productName; | |
private String productDescription; | |
private Double price; | |
private Date createDate; | |
... | |
} |
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
public class ProductController { | |
public ProductController(final ProductService productService) { | |
get("/products", (request, response) -> productService.getAllProduct(), json()); | |
get("/product/:id", (request, response) -> { | |
String id = request.params(":id"); | |
return productService.getProduct(id); | |
}, json()); | |
post("/product", (req, res) -> productService.createNewProduct( | |
req.queryParams("name"), | |
Double.valueOf(req.queryParams("price")) | |
), json()); | |
} | |
} |
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
public class ProductController { | |
public ProductController(final ProductService productService) { | |
get("/products", new Route() { | |
public Object handle(Request request, Response response) { | |
return JsonUtil.toJson(productService.getAllProduct()); | |
} | |
}); | |
get("/product/:id", new Route() { | |
public Object handle(Request request, Response response) { | |
Product product = productService.getProduct(request | |
.params(":id")); | |
return JsonUtil.toJson(product); | |
} | |
}); | |
post("/product", new Route() { | |
public Object handle(Request request, Response response) { | |
Product product = productService.createNewProduct( | |
request.queryParams("name"), | |
Double.valueOf(request.queryParams("price"))); | |
return JsonUtil.toJson(product); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment