Skip to content

Instantly share code, notes, and snippets.

@reinder42
Last active May 25, 2023 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reinder42/38f8e6f28ce88891f102856c5fc74d16 to your computer and use it in GitHub Desktop.
Save reinder42/38f8e6f28ce88891f102856c5fc74d16 to your computer and use it in GitHub Desktop.
Data retourneren met Jackson
package week4.college7.bigshopper.shopping.webservices;
import week4.college7.bigshopper.shopping.helpers.SimpleShoppingListResponse;
import week4.college7.bigshopper.shopping.model.Shop;
import week4.college7.bigshopper.shopping.model.ShoppingList;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Path("list")
public class ListResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getShoppingLists()
{
// Pak alle shopping lists
List<ShoppingList> shoppingLists = Shop.getShop().getAllShoppingLists();
// Return error response met Map.of()
if(shoppingLists.isEmpty()) {
// https://stackoverflow.com/questions/6802483/how-to-directly-initialize-a-hashmap-in-a-literal-way
Map<String, String> errorObject = Map.of("error", "no lists present");
return Response.ok(errorObject).build();
}
// Return de shopping lists
return Response.ok(shoppingLists).build();
// ... of:
// Mapping met map() naar SimpleShoppingListResponse
List<SimpleShoppingListResponse> responseObjects = shoppingLists.stream().map(
shoppingList -> SimpleShoppingListResponse.fromShoppingList(shoppingList)
).collect(Collectors.toUnmodifiableList());
return Response.ok(responseObjects).build();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{name}")
public Response getShoppingListByName(@PathParam("name") String name)
{
ShoppingList list = Shop.getShop().getShoppingListByName(name);
if(list == null) {
return Response.ok(Map.of("error", "no list by that name")).build();
}
return Response.ok(list).build();
}
}
// ...
@Path("shopper")
public class PersonResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Shopper> getShoppers() {
return Shop.getShop().getAllPersons();
}
// ...
}
// ...
@Path("product")
public class ProductResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getProducts() {
List<Product> products = Shop.getShop().getAllProducts();
if(products.isEmpty()) {
return Response.ok(Map.of("error", "no products available")).build();
}
// Transformeer lijst van objecten naar lijst van strings met map()
List<String> productNames = products.stream().map(product -> product.getName()).collect(Collectors.toList());
return Response.ok(productNames).build();
}
}
package week4.college7.bigshopper.shopping.helpers;
import javassist.Loader;
import week4.college7.bigshopper.shopping.model.Item;
import week4.college7.bigshopper.shopping.model.ShoppingList;
import java.util.List;
public class SimpleShoppingListResponse {
public String name;
public String owner;
public List<Item> listItems;
private SimpleShoppingListResponse(String name, String owner, List<Item> listItems) {
this.name = name;
this.owner = owner;
this.listItems = listItems;
}
public static SimpleShoppingListResponse fromShoppingList(ShoppingList shoppingList) {
SimpleShoppingListResponse responseObject = new SimpleShoppingListResponse(
shoppingList.getName(),
shoppingList.getOwner().getName(),
shoppingList.getListItems()
);
return responseObject;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment