GitBucket
4.21.2
Toggle navigation
Snippets
Sign in
Files
Branches
1
Releases
Issues
Pull requests
Labels
Priorities
Milestones
Wiki
Forks
mark.george
/
docker-demo
Browse code
Added location header to POST response.
master
1 parent
51ddf4a
commit
c05c7ceac4b75aa171d8dd8a6bdc1f214cacfa29
Mark George
authored
on 28 Feb 2018
Patch
Showing
2 changed files
src/resources/ShoppingListResource.java
src/service/Server.java
Ignore Space
Show notes
View
src/resources/ShoppingListResource.java
package resources; import dao.ShoppingDAO; import domain.ShoppingItem; import org.jooby.Jooby; import org.jooby.MediaType; import org.jooby.Status; public class ShoppingListResource extends Jooby { public ShoppingListResource(ShoppingDAO dao) { /** * A shopping list management service. */ path("/api/items", () -> { /** * Lists the items in the shopping list. * * @return Items in the list. */ get(() -> { return dao.getList(); }); /** * Add an item to the shopping list. * * @param body The item to add to the list. * @return 201 response if successful. */ post((req, rsp) -> { ShoppingItem item = req.body(ShoppingItem.class); dao.addItem(item); rsp.status(Status.CREATED) .header("location", "http://" + req.hostname()+":"+req.port()+""+req.path()+"/"+item.getName()); }); }).produces(MediaType.json).consumes(MediaType.json); } }
package resources; import dao.ShoppingDAO; import domain.ShoppingItem; import org.jooby.Jooby; import org.jooby.MediaType; import org.jooby.Status; public class ShoppingListResource extends Jooby { public ShoppingListResource(ShoppingDAO dao) { /** * A shopping list management service. */ path("/api/items", () -> { /** * Lists the items in the shopping list. * * @return Items in the list. */ get(() -> { return dao.getList(); }); /** * Add an item to the shopping list. * * @param body The item to add to the list. * @return 201 response if successful. */ post((req, rsp) -> { ShoppingItem item = req.body(ShoppingItem.class); dao.addItem(item); rsp.status(Status.CREATED); }); }).produces(MediaType.json).consumes(MediaType.json); } }
Ignore Space
Show notes
View
src/service/Server.java
/* */ package service; import dao.ShoppingDAO; import domain.ShoppingItem; import org.jooby.Jooby; import org.jooby.apitool.ApiTool; import org.jooby.json.Gzon; import resources.ShoppingItemResource; import resources.ShoppingListResource; /** * Shopping List Service. */ public class Server extends Jooby { public Server() { super("Server"); ShoppingDAO dao = new ShoppingDAO(); // add support for JSON use(new Gzon()); use(new ShoppingListResource(dao)); use(new ShoppingItemResource(dao)); // add Swagger documentation use(new ApiTool().modify(r -> r.pattern().equals("/api/items"), route -> { // Fix response type since Swagger couldn't guess route.response().type(new ShoppingItem[0].getClass()); }).swagger()); } public static void main(String[] args) { new Server().start(); } }
/* */ package service; import dao.ShoppingDAO; import domain.ShoppingItem; import java.util.ArrayList; import org.jooby.Jooby; import org.jooby.apitool.ApiTool; import org.jooby.json.Gzon; import resources.ShoppingItemResource; import resources.ShoppingListResource; /** * Shopping List Service. */ public class Server extends Jooby { public Server() { super("Server"); ShoppingDAO dao = new ShoppingDAO(); // add support for JSON use(new Gzon()); use(new ShoppingListResource(dao)); use(new ShoppingItemResource(dao)); // add Swagger documentation use(new ApiTool().modify(r -> r.pattern().equals("/api/items"), route -> { // Fix response type since Swagger couldn't guess route.response().type(new ShoppingItem[0].getClass()); }).swagger()); } public static void main(String[] args) { new Server().start(); } }
Show line notes below