Skip to content

Commit 4f410a7

Browse files
hateoas - minor changes for self and method link creation
1 parent 398b0e3 commit 4f410a7

3 files changed

Lines changed: 39 additions & 36 deletions

File tree

‎spring-security-rest/pom.xml‎

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<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">
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
23
<modelVersion>4.0.0</modelVersion>
34
<groupId>com.baeldung</groupId>
45
<artifactId>spring-security-rest</artifactId>
@@ -76,14 +77,13 @@
7677
<artifactId>spring-webmvc</artifactId>
7778
<version>${org.springframework.version}</version>
7879
</dependency>
79-
80+
8081
<!-- Spring HATEOAS -->
81-
82-
<dependency>
83-
<groupId>org.springframework.hateoas</groupId>
84-
<artifactId>spring-hateoas</artifactId>
85-
<version>0.19.0.RELEASE</version>
86-
</dependency>
82+
<dependency>
83+
<groupId>org.springframework.hateoas</groupId>
84+
<artifactId>spring-hateoas</artifactId>
85+
<version>0.19.0.RELEASE</version>
86+
</dependency>
8787

8888
<!-- web -->
8989

@@ -100,13 +100,13 @@
100100
<version>${jstl.version}</version>
101101
<scope>runtime</scope>
102102
</dependency>
103-
103+
104104
<dependency>
105-
<groupId>javax.validation</groupId>
106-
<artifactId>validation-api</artifactId>
107-
<version>${javax.validation.version}</version>
108-
</dependency>
109-
105+
<groupId>javax.validation</groupId>
106+
<artifactId>validation-api</artifactId>
107+
<version>${javax.validation.version}</version>
108+
</dependency>
109+
110110

111111
<!-- marshalling -->
112112

@@ -160,14 +160,14 @@
160160
<version>${org.springframework.version}</version>
161161
<scope>test</scope>
162162
</dependency>
163-
163+
164164
<dependency>
165-
<groupId>org.springframework.security</groupId>
166-
<artifactId>spring-security-test</artifactId>
167-
<version>${org.springframework.security.version}</version>
168-
</dependency>
169-
170-
165+
<groupId>org.springframework.security</groupId>
166+
<artifactId>spring-security-test</artifactId>
167+
<version>${org.springframework.security.version}</version>
168+
</dependency>
169+
170+
171171
<dependency>
172172
<groupId>com.jayway.restassured</groupId>
173173
<artifactId>rest-assured</artifactId>
@@ -180,7 +180,7 @@
180180
</exclusion>
181181
</exclusions>
182182
</dependency>
183-
183+
184184
<dependency>
185185
<groupId>junit</groupId>
186186
<artifactId>junit</artifactId>
@@ -269,7 +269,6 @@
269269
<artifactId>cargo-maven2-plugin</artifactId>
270270
<version>${cargo-maven2-plugin.version}</version>
271271
<configuration>
272-
<wait>true</wait>
273272
<container>
274273
<containerId>jetty8x</containerId>
275274
<type>embedded</type>
@@ -309,7 +308,7 @@
309308
<jstl.version>1.2</jstl.version>
310309
<jackson.version>2.2.2</jackson.version>
311310
<springfox.version>2.2.2</springfox.version>
312-
311+
313312
<!-- util -->
314313
<guava.version>19.0</guava.version>
315314
<commons-lang3.version>3.4</commons-lang3.version>

‎spring-security-rest/src/main/java/org/baeldung/spring/SecurityJavaConfig.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ protected void configure(final HttpSecurity http) throws Exception {// @formatte
4343
.and()
4444
.authorizeRequests()
4545
.antMatchers("/api/csrfAttacker*").permitAll()
46-
.antMatchers("/api/customers**").permitAll()
4746
.antMatchers("/api/customer/**").permitAll()
4847
.antMatchers("/api/**").authenticated()
4948
.and()

‎spring-security-rest/src/main/java/org/baeldung/web/controller/CustomerController.java‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,60 @@
11
package org.baeldung.web.controller;
22

3-
import java.util.List;
4-
53
import org.baeldung.persistence.model.Customer;
64
import org.baeldung.persistence.model.Order;
75
import org.baeldung.web.service.CustomerService;
86
import org.baeldung.web.service.OrderService;
97
import org.springframework.beans.factory.annotation.Autowired;
108
import org.springframework.hateoas.Link;
11-
import org.springframework.hateoas.mvc.ControllerLinkBuilder;
129
import org.springframework.web.bind.annotation.PathVariable;
1310
import org.springframework.web.bind.annotation.RequestMapping;
1411
import org.springframework.web.bind.annotation.RequestMethod;
1512
import org.springframework.web.bind.annotation.RestController;
1613

14+
import java.util.List;
15+
16+
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
17+
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;
18+
1719
@RestController
20+
@RequestMapping(value = "/customer")
1821
public class CustomerController {
1922
@Autowired
2023
private CustomerService customerService;
2124

2225
@Autowired
2326
private OrderService orderService;
2427

25-
@RequestMapping(value = "/customer/{customerId}", method = RequestMethod.GET)
28+
@RequestMapping(value = "/{customerId}", method = RequestMethod.GET)
2629
public Customer getCustomerById(@PathVariable final String customerId) {
2730
return customerService.getCustomerDetail(customerId);
2831
}
2932

30-
@RequestMapping(value = "/customer/{customerId}/{orderId}", method = RequestMethod.GET)
33+
@RequestMapping(value = "/{customerId}/{orderId}", method = RequestMethod.GET)
3134
public Order getOrderById(@PathVariable final String customerId, @PathVariable final String orderId) {
3235
return orderService.getOrderByIdForCustomer(customerId, orderId);
3336
}
3437

35-
@RequestMapping(value = "/customer/{customerId}/orders", method = RequestMethod.GET)
38+
@RequestMapping(value = "/{customerId}/orders", method = RequestMethod.GET)
3639
public List<Order> getOrdersForCustomer(@PathVariable final String customerId) {
3740
final List<Order> orders = orderService.getAllOrdersForCustomer(customerId);
3841
for (final Order order : orders) {
39-
final Link selfLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrderById(customerId, order.getOrderId())).withSelfRel();
42+
final Link selfLink = linkTo(methodOn(CustomerController.class).getOrderById(customerId, order.getOrderId())).withSelfRel();
4043
order.add(selfLink);
4144
}
4245
return orders;
4346
}
4447

45-
@RequestMapping(value = "/customers", method = RequestMethod.GET)
48+
@RequestMapping(method = RequestMethod.GET)
4649
public List<Customer> getAllCustomers() {
4750
final List<Customer> allCustomers = customerService.allCustomers();
4851
for (final Customer customer : allCustomers) {
49-
final Link selfLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getCustomerById(customer.getCustomerId())).withSelfRel();
52+
String customerId = customer.getCustomerId();
53+
Link selfLink = linkTo(CustomerController.class).slash(customerId).withSelfRel();
5054
customer.add(selfLink);
51-
if (orderService.getAllOrdersForCustomer(customer.getCustomerId()).size() > 0) {
52-
final Link ordersLink = ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(CustomerController.class).getOrdersForCustomer(customer.getCustomerId())).withRel("allOrders");
55+
if (orderService.getAllOrdersForCustomer(customerId).size() > 0) {
56+
List<Order> methodLinkBuilder = methodOn(CustomerController.class).getOrdersForCustomer(customerId);
57+
final Link ordersLink = linkTo(methodLinkBuilder).withRel("allOrders");
5358
customer.add(ordersLink);
5459
}
5560

0 commit comments

Comments
 (0)