Skip to content

Commit 4ed42aa

Browse files
committed
Merge branch '1.1.x'
2 parents e57b0e0 + 7d017a2 commit 4ed42aa

File tree

8 files changed

+326
-0
lines changed

8 files changed

+326
-0
lines changed

spring-boot-samples/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<module>spring-boot-sample-data-rest</module>
3737
<module>spring-boot-sample-data-solr</module>
3838
<module>spring-boot-sample-flyway</module>
39+
<module>spring-boot-sample-hateoas</module>
3940
<module>spring-boot-sample-hornetq</module>
4041
<module>spring-boot-sample-integration</module>
4142
<module>spring-boot-sample-jersey</module>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<!-- Your own application should inherit from spring-boot-starter-parent -->
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-samples</artifactId>
8+
<version>1.1.11.BUILD-SNAPSHOT</version>
9+
</parent>
10+
<artifactId>spring-boot-sample-hateoas</artifactId>
11+
<name>Spring Boot Hateoas Sample</name>
12+
<description>Spring Boot Hateoas Sample</description>
13+
<url>http://projects.spring.io/spring-boot/</url>
14+
<organization>
15+
<name>Pivotal Software, Inc.</name>
16+
<url>http://www.spring.io</url>
17+
</organization>
18+
<properties>
19+
<main.basedir>${basedir}/../..</main.basedir>
20+
</properties>
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.hateoas</groupId>
28+
<artifactId>spring-hateoas</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.plugin</groupId>
32+
<artifactId>spring-plugin-core</artifactId>
33+
<version>1.1.0.RELEASE</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-starter-test</artifactId>
38+
<scope>test</scope>
39+
</dependency>
40+
</dependencies>
41+
<build>
42+
<plugins>
43+
<plugin>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-maven-plugin</artifactId>
46+
</plugin>
47+
</plugins>
48+
</build>
49+
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample;
18+
19+
import org.springframework.boot.SpringApplication;
20+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
21+
import org.springframework.context.annotation.ComponentScan;
22+
import org.springframework.context.annotation.Configuration;
23+
24+
@Configuration
25+
@EnableAutoConfiguration
26+
@ComponentScan
27+
public class SampleHateoasApplication {
28+
29+
public static void main(String[] args) {
30+
SpringApplication.run(SampleHateoasApplication.class, args);
31+
}
32+
33+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.domain;
18+
19+
public class Customer {
20+
21+
private final Long id;
22+
23+
private final String firstName;
24+
25+
private final String lastName;
26+
27+
public Customer(Long id, String firstName, String lastName) {
28+
this.id = id;
29+
this.firstName = firstName;
30+
this.lastName = lastName;
31+
}
32+
33+
public Long getId() {
34+
return this.id;
35+
}
36+
37+
public String getFirstName() {
38+
return this.firstName;
39+
}
40+
41+
public String getLastName() {
42+
return this.lastName;
43+
}
44+
45+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.domain;
18+
19+
import java.util.List;
20+
21+
public interface CustomerRepository {
22+
23+
List<Customer> findAll();
24+
25+
Customer findOne(Long id);
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.domain;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import org.springframework.stereotype.Repository;
23+
import org.springframework.util.ObjectUtils;
24+
25+
@Repository
26+
public class InMemoryCustomerRepository implements CustomerRepository {
27+
28+
private final List<Customer> customers = new ArrayList<Customer>();
29+
30+
public InMemoryCustomerRepository() {
31+
this.customers.add(new Customer(1L, "Oliver", "Gierke"));
32+
this.customers.add(new Customer(2L, "Andy", "Wilkinson"));
33+
this.customers.add(new Customer(2L, "Dave", "Syer"));
34+
}
35+
36+
@Override
37+
public List<Customer> findAll() {
38+
return this.customers;
39+
}
40+
41+
@Override
42+
public Customer findOne(Long id) {
43+
for (Customer customer : this.customers) {
44+
if (ObjectUtils.nullSafeEquals(customer.getId(), id)) {
45+
return customer;
46+
}
47+
}
48+
return null;
49+
}
50+
51+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.web;
18+
19+
import org.springframework.beans.factory.annotation.Autowired;
20+
import org.springframework.hateoas.EntityLinks;
21+
import org.springframework.hateoas.ExposesResourceFor;
22+
import org.springframework.hateoas.Resource;
23+
import org.springframework.hateoas.Resources;
24+
import org.springframework.http.HttpEntity;
25+
import org.springframework.http.HttpStatus;
26+
import org.springframework.http.ResponseEntity;
27+
import org.springframework.stereotype.Controller;
28+
import org.springframework.web.bind.annotation.PathVariable;
29+
import org.springframework.web.bind.annotation.RequestMapping;
30+
import org.springframework.web.bind.annotation.RequestMethod;
31+
32+
import sample.domain.Customer;
33+
import sample.domain.CustomerRepository;
34+
35+
@Controller
36+
@RequestMapping("/customers")
37+
@ExposesResourceFor(Customer.class)
38+
public class CustomerController {
39+
40+
private final CustomerRepository repository;
41+
42+
private final EntityLinks entityLinks;
43+
44+
@Autowired
45+
public CustomerController(CustomerRepository repository, EntityLinks entityLinks) {
46+
this.repository = repository;
47+
this.entityLinks = entityLinks;
48+
}
49+
50+
@RequestMapping(method = RequestMethod.GET)
51+
HttpEntity<Resources<Customer>> showCustomers() {
52+
Resources<Customer> resources = new Resources<Customer>(this.repository.findAll());
53+
resources.add(this.entityLinks.linkToCollectionResource(Customer.class));
54+
return new ResponseEntity<Resources<Customer>>(resources, HttpStatus.OK);
55+
}
56+
57+
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
58+
HttpEntity<Resource<Customer>> showCustomer(@PathVariable Long id) {
59+
Resource<Customer> resource = new Resource<Customer>(this.repository.findOne(id));
60+
resource.add(this.entityLinks.linkToSingleResource(Customer.class, id));
61+
return new ResponseEntity<Resource<Customer>>(resource, HttpStatus.OK);
62+
}
63+
64+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
import org.springframework.beans.factory.annotation.Value;
22+
import org.springframework.boot.test.IntegrationTest;
23+
import org.springframework.boot.test.SpringApplicationConfiguration;
24+
import org.springframework.boot.test.TestRestTemplate;
25+
import org.springframework.http.HttpStatus;
26+
import org.springframework.http.ResponseEntity;
27+
import org.springframework.test.annotation.DirtiesContext;
28+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
29+
import org.springframework.test.context.web.WebAppConfiguration;
30+
31+
import static org.hamcrest.Matchers.containsString;
32+
import static org.hamcrest.Matchers.equalTo;
33+
import static org.hamcrest.Matchers.startsWith;
34+
import static org.junit.Assert.assertThat;
35+
36+
@RunWith(SpringJUnit4ClassRunner.class)
37+
@SpringApplicationConfiguration(classes = SampleHateoasApplication.class)
38+
@WebAppConfiguration
39+
@IntegrationTest("server.port:0")
40+
@DirtiesContext
41+
public class SampleHateoasApplicationTests {
42+
43+
@Value("${local.server.port}")
44+
private int port;
45+
46+
@Test
47+
public void hasHalLinks() throws Exception {
48+
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
49+
"http://localhost:" + this.port + "/customers/1", String.class);
50+
assertThat(entity.getStatusCode(), equalTo(HttpStatus.OK));
51+
assertThat(entity.getBody(), startsWith("{\"id\":1,\"firstName\":\"Oliver\""
52+
+ ",\"lastName\":\"Gierke\""));
53+
assertThat(entity.getBody(), containsString("_links\":{\"self\":{\"href\""));
54+
}
55+
56+
}

0 commit comments

Comments
 (0)