Merge pull request #6745 from rozagerardo/geroza/BAEL-14138_update-and-move-Spring-HAETOAS-article
[BAEL-14138] Update and move code for the "Spring HATEOAS" article
This commit is contained in:
commit
5e859a117f
|
@ -7,10 +7,12 @@ Module for the articles that are part of the Spring REST E-book:
|
|||
5. [Entity To DTO Conversion for a Spring REST API](https://www.baeldung.com/entity-to-and-from-dto-for-a-java-spring-application)
|
||||
6. [Error Handling for REST with Spring](http://www.baeldung.com/exception-handling-for-rest-with-spring)
|
||||
7. [REST API Discoverability and HATEOAS](http://www.baeldung.com/restful-web-service-discoverability)
|
||||
8. [REST Pagination in Spring](http://www.baeldung.com/rest-api-pagination-in-spring)
|
||||
9. [Test a REST API with Java](http://www.baeldung.com/integration-testing-a-rest-api)
|
||||
10. [HATEOAS for a Spring REST Service](http://www.baeldung.com/rest-api-discoverability-with-spring)
|
||||
11. [Versioning a REST API](http://www.baeldung.com/rest-versioning)
|
||||
12. [ETags for REST with Spring](http://www.baeldung.com/etags-for-rest-with-spring)
|
||||
13. [Testing REST with multiple MIME types](http://www.baeldung.com/testing-rest-api-with-multiple-media-types)
|
||||
14. [Testing Web APIs with Postman Collections](https://www.baeldung.com/postman-testing-collections)
|
||||
8. [An Intro to Spring HATEOAS](http://www.baeldung.com/spring-hateoas-tutorial)
|
||||
9. [REST Pagination in Spring](http://www.baeldung.com/rest-api-pagination-in-spring)
|
||||
10. [Test a REST API with Java](http://www.baeldung.com/integration-testing-a-rest-api)
|
||||
|
||||
- [HATEOAS for a Spring REST Service](http://www.baeldung.com/rest-api-discoverability-with-spring)
|
||||
- [Versioning a REST API](http://www.baeldung.com/rest-versioning)
|
||||
- [ETags for REST with Spring](http://www.baeldung.com/etags-for-rest-with-spring)
|
||||
- [Testing REST with multiple MIME types](http://www.baeldung.com/testing-rest-api-with-multiple-media-types)
|
||||
- [Testing Web APIs with Postman Collections](https://www.baeldung.com/postman-testing-collections)
|
||||
|
|
|
@ -44,6 +44,12 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring HATEOAS -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-hateoas</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- util -->
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.persistence.model;
|
||||
package com.baeldung.persistence.model;
|
||||
|
||||
import java.util.Map;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.persistence.model;
|
||||
package com.baeldung.persistence.model;
|
||||
|
||||
import org.springframework.hateoas.ResourceSupport;
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
package org.baeldung.web.service;
|
||||
package com.baeldung.services;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Customer;
|
||||
import com.baeldung.persistence.model.Customer;
|
||||
|
||||
public interface CustomerService {
|
||||
|
|
@ -1,12 +1,13 @@
|
|||
package org.baeldung.web.service;
|
||||
package com.baeldung.services;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Customer;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.persistence.model.Customer;
|
||||
|
||||
@Service
|
||||
public class CustomerServiceImpl implements CustomerService {
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
package org.baeldung.web.service;
|
||||
package com.baeldung.services;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Order;
|
||||
import com.baeldung.persistence.model.Order;
|
||||
|
||||
public interface OrderService {
|
||||
|
|
@ -1,14 +1,15 @@
|
|||
package org.baeldung.web.service;
|
||||
package com.baeldung.services;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.baeldung.persistence.model.Customer;
|
||||
import org.baeldung.persistence.model.Order;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.persistence.model.Customer;
|
||||
import com.baeldung.persistence.model.Order;
|
||||
|
||||
@Service
|
||||
public class OrderServiceImpl implements OrderService {
|
||||
|
|
@ -1,24 +1,25 @@
|
|||
package org.baeldung.web.controller;
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
|
||||
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Customer;
|
||||
import org.baeldung.persistence.model.Order;
|
||||
import org.baeldung.web.service.CustomerService;
|
||||
import org.baeldung.web.service.OrderService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.Resources;
|
||||
import org.springframework.hateoas.config.EnableHypermediaSupport;
|
||||
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.persistence.model.Customer;
|
||||
import com.baeldung.persistence.model.Order;
|
||||
import com.baeldung.services.CustomerService;
|
||||
import com.baeldung.services.OrderService;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/customers")
|
||||
@EnableHypermediaSupport(type = HypermediaType.HAL)
|
||||
|
@ -29,45 +30,49 @@ public class CustomerController {
|
|||
@Autowired
|
||||
private OrderService orderService;
|
||||
|
||||
@RequestMapping(value = "/{customerId}", method = RequestMethod.GET)
|
||||
@GetMapping("/{customerId}")
|
||||
public Customer getCustomerById(@PathVariable final String customerId) {
|
||||
return customerService.getCustomerDetail(customerId);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/{customerId}/{orderId}", method = RequestMethod.GET)
|
||||
@GetMapping("/{customerId}/{orderId}")
|
||||
public Order getOrderById(@PathVariable final String customerId, @PathVariable final String orderId) {
|
||||
return orderService.getOrderByIdForCustomer(customerId, orderId);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/{customerId}/orders", method = RequestMethod.GET , produces = {"application/hal+json"})
|
||||
@GetMapping(value = "/{customerId}/orders", produces = { "application/hal+json" })
|
||||
public Resources<Order> getOrdersForCustomer(@PathVariable final String customerId) {
|
||||
final List<Order> orders = orderService.getAllOrdersForCustomer(customerId);
|
||||
for (final Order order : orders) {
|
||||
final Link selfLink = linkTo(methodOn(CustomerController.class).getOrderById(customerId, order.getOrderId())).withSelfRel();
|
||||
final Link selfLink = linkTo(
|
||||
methodOn(CustomerController.class).getOrderById(customerId, order.getOrderId())).withSelfRel();
|
||||
order.add(selfLink);
|
||||
}
|
||||
|
||||
Link link =linkTo(methodOn(CustomerController.class).getOrdersForCustomer(customerId)).withSelfRel();
|
||||
Resources<Order> result = new Resources<>(orders,link);
|
||||
|
||||
Link link = linkTo(methodOn(CustomerController.class).getOrdersForCustomer(customerId)).withSelfRel();
|
||||
Resources<Order> result = new Resources<>(orders, link);
|
||||
return result;
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, produces = {"application/hal+json"})
|
||||
@GetMapping(produces = { "application/hal+json" })
|
||||
public Resources<Customer> getAllCustomers() {
|
||||
final List<Customer> allCustomers = customerService.allCustomers();
|
||||
|
||||
|
||||
for (final Customer customer : allCustomers) {
|
||||
String customerId = customer.getCustomerId();
|
||||
Link selfLink = linkTo(CustomerController.class).slash(customerId).withSelfRel();
|
||||
Link selfLink = linkTo(CustomerController.class).slash(customerId)
|
||||
.withSelfRel();
|
||||
customer.add(selfLink);
|
||||
if (orderService.getAllOrdersForCustomer(customerId).size() > 0) {
|
||||
final Link ordersLink = linkTo(methodOn(CustomerController.class).getOrdersForCustomer(customerId)).withRel("allOrders");
|
||||
if (orderService.getAllOrdersForCustomer(customerId)
|
||||
.size() > 0) {
|
||||
final Link ordersLink = linkTo(methodOn(CustomerController.class).getOrdersForCustomer(customerId))
|
||||
.withRel("allOrders");
|
||||
customer.add(ordersLink);
|
||||
}
|
||||
}
|
||||
|
||||
Link link =linkTo(CustomerController.class).withSelfRel();
|
||||
Resources<Customer> result = new Resources<>(allCustomers,link);
|
||||
|
||||
Link link = linkTo(CustomerController.class).withSelfRel();
|
||||
Resources<Customer> result = new Resources<>(allCustomers, link);
|
||||
return result;
|
||||
}
|
||||
|
|
@ -0,0 +1,98 @@
|
|||
package com.baeldung.springhateoas;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.hateoas.MediaTypes;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import com.baeldung.persistence.model.Customer;
|
||||
import com.baeldung.persistence.model.Order;
|
||||
import com.baeldung.services.CustomerService;
|
||||
import com.baeldung.services.OrderService;
|
||||
import com.baeldung.web.controller.CustomerController;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@WebMvcTest(CustomerController.class)
|
||||
public class CustomerControllerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mvc;
|
||||
|
||||
@MockBean
|
||||
private CustomerService customerService;
|
||||
|
||||
@MockBean
|
||||
private OrderService orderService;
|
||||
|
||||
private static final String DEFAULT_CUSTOMER_ID = "customer1";
|
||||
private static final String DEFAULT_ORDER_ID = "order1";
|
||||
|
||||
@Test
|
||||
public void givenExistingCustomer_whenCustomerRequested_thenResourceRetrieved() throws Exception {
|
||||
given(this.customerService.getCustomerDetail(DEFAULT_CUSTOMER_ID))
|
||||
.willReturn(new Customer(DEFAULT_CUSTOMER_ID, "customerJohn", "companyOne"));
|
||||
|
||||
this.mvc.perform(get("/customers/" + DEFAULT_CUSTOMER_ID))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$._links").doesNotExist())
|
||||
.andExpect(jsonPath("$.customerId", is(DEFAULT_CUSTOMER_ID)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingOrder_whenOrderRequested_thenResourceRetrieved() throws Exception {
|
||||
given(this.orderService.getOrderByIdForCustomer(DEFAULT_CUSTOMER_ID, DEFAULT_ORDER_ID))
|
||||
.willReturn(new Order(DEFAULT_ORDER_ID, 1., 1));
|
||||
|
||||
this.mvc.perform(get("/customers/" + DEFAULT_CUSTOMER_ID + "/" + DEFAULT_ORDER_ID))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$._links").doesNotExist())
|
||||
.andExpect(jsonPath("$.orderId", is(DEFAULT_ORDER_ID)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingCustomerWithOrders_whenOrdersRequested_thenHalResourceRetrieved() throws Exception {
|
||||
Order order1 = new Order(DEFAULT_ORDER_ID, 1., 1);
|
||||
List<Order> orders = Collections.singletonList(order1);
|
||||
given(this.orderService.getAllOrdersForCustomer(DEFAULT_CUSTOMER_ID)).willReturn(orders);
|
||||
|
||||
this.mvc.perform(get("/customers/" + DEFAULT_CUSTOMER_ID + "/orders").accept(MediaTypes.HAL_JSON_VALUE))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$._embedded.orderList[0]._links.self.href",
|
||||
is("http://localhost/customers/customer1/order1")))
|
||||
.andExpect(jsonPath("$._links.self.href", is("http://localhost/customers/customer1/orders")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingCustomer_whenAllCustomersRequested_thenHalResourceRetrieved() throws Exception {
|
||||
// customers
|
||||
Customer retrievedCustomer = new Customer(DEFAULT_CUSTOMER_ID, "customerJohn", "companyOne");
|
||||
List<Customer> customers = Collections.singletonList(retrievedCustomer);
|
||||
given(this.customerService.allCustomers()).willReturn(customers);
|
||||
// orders
|
||||
Order order1 = new Order(DEFAULT_ORDER_ID, 1., 1);
|
||||
List<Order> orders = Collections.singletonList(order1);
|
||||
given(this.orderService.getAllOrdersForCustomer(DEFAULT_CUSTOMER_ID)).willReturn(orders);
|
||||
|
||||
this.mvc.perform(get("/customers/").accept(MediaTypes.HAL_JSON_VALUE))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(
|
||||
jsonPath("$._embedded.customerList[0]._links.self.href", is("http://localhost/customers/customer1")))
|
||||
.andExpect(jsonPath("$._embedded.customerList[0]._links.allOrders.href",
|
||||
is("http://localhost/customers/customer1/orders")))
|
||||
.andExpect(jsonPath("$._links.self.href", is("http://localhost/customers")));
|
||||
}
|
||||
|
||||
}
|
|
@ -11,7 +11,6 @@ The "Learn Spring Security" Classes: http://github.learnspringsecurity.com
|
|||
- [Spring REST Service Security](http://www.baeldung.com/2011/10/31/securing-a-restful-web-service-with-spring-security-3-1-part-3/)
|
||||
- [Setting Up Swagger 2 with a Spring REST API](http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api)
|
||||
- [Custom Error Message Handling for REST API](http://www.baeldung.com/global-error-handler-in-a-spring-rest-api)
|
||||
- [An Intro to Spring HATEOAS](http://www.baeldung.com/spring-hateoas-tutorial)
|
||||
- [Spring Security Context Propagation with @Async](http://www.baeldung.com/spring-security-async-principal-propagation)
|
||||
- [Servlet 3 Async Support with Spring MVC and Spring Security](http://www.baeldung.com/spring-mvc-async-security)
|
||||
- [Intro to Spring Security Expressions](http://www.baeldung.com/spring-security-expressions)
|
||||
|
|
|
@ -84,13 +84,6 @@
|
|||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring HATEOAS -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.hateoas</groupId>
|
||||
<artifactId>spring-hateoas</artifactId>
|
||||
<version>${org.springframework.hateoas.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- web -->
|
||||
|
||||
<dependency>
|
||||
|
@ -273,9 +266,6 @@
|
|||
</profiles>
|
||||
|
||||
<properties>
|
||||
<!-- Spring -->
|
||||
<org.springframework.hateoas.version>0.25.0.RELEASE</org.springframework.hateoas.version>
|
||||
|
||||
<!-- various -->
|
||||
<javax.servlet-api.version>3.1.0</javax.servlet-api.version>
|
||||
<javax.validation.version>1.1.0.Final</javax.validation.version>
|
||||
|
|
Loading…
Reference in New Issue