Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
7d017a2a
Commit
7d017a2a
authored
Dec 15, 2014
by
Phillip Webb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add HATEOAS sample application
parent
e72f3c02
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
326 additions
and
0 deletions
+326
-0
pom.xml
spring-boot-samples/pom.xml
+1
-0
pom.xml
spring-boot-samples/spring-boot-sample-hateoas/pom.xml
+49
-0
SampleHateoasApplication.java
...ateoas/src/main/java/sample/SampleHateoasApplication.java
+33
-0
Customer.java
...-sample-hateoas/src/main/java/sample/domain/Customer.java
+45
-0
CustomerRepository.java
...teoas/src/main/java/sample/domain/CustomerRepository.java
+27
-0
InMemoryCustomerRepository.java
...c/main/java/sample/domain/InMemoryCustomerRepository.java
+51
-0
CustomerController.java
...-hateoas/src/main/java/sample/web/CustomerController.java
+64
-0
SampleHateoasApplicationTests.java
...s/src/test/java/sample/SampleHateoasApplicationTests.java
+56
-0
No files found.
spring-boot-samples/pom.xml
View file @
7d017a2a
...
...
@@ -35,6 +35,7 @@
<module>
spring-boot-sample-data-rest
</module>
<module>
spring-boot-sample-data-solr
</module>
<module>
spring-boot-sample-flyway
</module>
<module>
spring-boot-sample-hateoas
</module>
<module>
spring-boot-sample-hornetq
</module>
<module>
spring-boot-sample-integration
</module>
<module>
spring-boot-sample-jetty
</module>
...
...
spring-boot-samples/spring-boot-sample-hateoas/pom.xml
0 → 100644
View file @
7d017a2a
<?xml version="1.0" encoding="UTF-8"?>
<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"
>
<modelVersion>
4.0.0
</modelVersion>
<parent>
<!-- Your own application should inherit from spring-boot-starter-parent -->
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-samples
</artifactId>
<version>
1.1.11.BUILD-SNAPSHOT
</version>
</parent>
<artifactId>
spring-boot-sample-hateoas
</artifactId>
<name>
Spring Boot Hateoas Sample
</name>
<description>
Spring Boot Hateoas Sample
</description>
<url>
http://projects.spring.io/spring-boot/
</url>
<organization>
<name>
Pivotal Software, Inc.
</name>
<url>
http://www.spring.io
</url>
</organization>
<properties>
<main.basedir>
${basedir}/../..
</main.basedir>
</properties>
<dependencies>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-web
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.hateoas
</groupId>
<artifactId>
spring-hateoas
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.plugin
</groupId>
<artifactId>
spring-plugin-core
</artifactId>
<version>
1.1.0.RELEASE
</version>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
<scope>
test
</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-maven-plugin
</artifactId>
</plugin>
</plugins>
</build>
</project>
spring-boot-samples/spring-boot-sample-hateoas/src/main/java/sample/SampleHateoasApplication.java
0 → 100644
View file @
7d017a2a
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
sample
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.EnableAutoConfiguration
;
import
org.springframework.context.annotation.ComponentScan
;
import
org.springframework.context.annotation.Configuration
;
@Configuration
@EnableAutoConfiguration
@ComponentScan
public
class
SampleHateoasApplication
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
SampleHateoasApplication
.
class
,
args
);
}
}
spring-boot-samples/spring-boot-sample-hateoas/src/main/java/sample/domain/Customer.java
0 → 100644
View file @
7d017a2a
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
sample
.
domain
;
public
class
Customer
{
private
final
Long
id
;
private
final
String
firstName
;
private
final
String
lastName
;
public
Customer
(
Long
id
,
String
firstName
,
String
lastName
)
{
this
.
id
=
id
;
this
.
firstName
=
firstName
;
this
.
lastName
=
lastName
;
}
public
Long
getId
()
{
return
this
.
id
;
}
public
String
getFirstName
()
{
return
this
.
firstName
;
}
public
String
getLastName
()
{
return
this
.
lastName
;
}
}
spring-boot-samples/spring-boot-sample-hateoas/src/main/java/sample/domain/CustomerRepository.java
0 → 100644
View file @
7d017a2a
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
sample
.
domain
;
import
java.util.List
;
public
interface
CustomerRepository
{
List
<
Customer
>
findAll
();
Customer
findOne
(
Long
id
);
}
spring-boot-samples/spring-boot-sample-hateoas/src/main/java/sample/domain/InMemoryCustomerRepository.java
0 → 100644
View file @
7d017a2a
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
sample
.
domain
;
import
java.util.ArrayList
;
import
java.util.List
;
import
org.springframework.stereotype.Repository
;
import
org.springframework.util.ObjectUtils
;
@Repository
public
class
InMemoryCustomerRepository
implements
CustomerRepository
{
private
final
List
<
Customer
>
customers
=
new
ArrayList
<
Customer
>();
public
InMemoryCustomerRepository
()
{
this
.
customers
.
add
(
new
Customer
(
1L
,
"Oliver"
,
"Gierke"
));
this
.
customers
.
add
(
new
Customer
(
2L
,
"Andy"
,
"Wilkinson"
));
this
.
customers
.
add
(
new
Customer
(
2L
,
"Dave"
,
"Syer"
));
}
@Override
public
List
<
Customer
>
findAll
()
{
return
this
.
customers
;
}
@Override
public
Customer
findOne
(
Long
id
)
{
for
(
Customer
customer
:
this
.
customers
)
{
if
(
ObjectUtils
.
nullSafeEquals
(
customer
.
getId
(),
id
))
{
return
customer
;
}
}
return
null
;
}
}
spring-boot-samples/spring-boot-sample-hateoas/src/main/java/sample/web/CustomerController.java
0 → 100644
View file @
7d017a2a
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
sample
.
web
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.hateoas.EntityLinks
;
import
org.springframework.hateoas.ExposesResourceFor
;
import
org.springframework.hateoas.Resource
;
import
org.springframework.hateoas.Resources
;
import
org.springframework.http.HttpEntity
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
sample.domain.Customer
;
import
sample.domain.CustomerRepository
;
@Controller
@RequestMapping
(
"/customers"
)
@ExposesResourceFor
(
Customer
.
class
)
public
class
CustomerController
{
private
final
CustomerRepository
repository
;
private
final
EntityLinks
entityLinks
;
@Autowired
public
CustomerController
(
CustomerRepository
repository
,
EntityLinks
entityLinks
)
{
this
.
repository
=
repository
;
this
.
entityLinks
=
entityLinks
;
}
@RequestMapping
(
method
=
RequestMethod
.
GET
)
HttpEntity
<
Resources
<
Customer
>>
showCustomers
()
{
Resources
<
Customer
>
resources
=
new
Resources
<
Customer
>(
this
.
repository
.
findAll
());
resources
.
add
(
this
.
entityLinks
.
linkToCollectionResource
(
Customer
.
class
));
return
new
ResponseEntity
<
Resources
<
Customer
>>(
resources
,
HttpStatus
.
OK
);
}
@RequestMapping
(
value
=
"/{id}"
,
method
=
RequestMethod
.
GET
)
HttpEntity
<
Resource
<
Customer
>>
showCustomer
(
@PathVariable
Long
id
)
{
Resource
<
Customer
>
resource
=
new
Resource
<
Customer
>(
this
.
repository
.
findOne
(
id
));
resource
.
add
(
this
.
entityLinks
.
linkToSingleResource
(
Customer
.
class
,
id
));
return
new
ResponseEntity
<
Resource
<
Customer
>>(
resource
,
HttpStatus
.
OK
);
}
}
spring-boot-samples/spring-boot-sample-hateoas/src/test/java/sample/SampleHateoasApplicationTests.java
0 → 100644
View file @
7d017a2a
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
sample
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.test.IntegrationTest
;
import
org.springframework.boot.test.SpringApplicationConfiguration
;
import
org.springframework.boot.test.TestRestTemplate
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.test.annotation.DirtiesContext
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.context.web.WebAppConfiguration
;
import
static
org
.
hamcrest
.
Matchers
.
containsString
;
import
static
org
.
hamcrest
.
Matchers
.
equalTo
;
import
static
org
.
hamcrest
.
Matchers
.
startsWith
;
import
static
org
.
junit
.
Assert
.
assertThat
;
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@SpringApplicationConfiguration
(
classes
=
SampleHateoasApplication
.
class
)
@WebAppConfiguration
@IntegrationTest
(
"server.port:0"
)
@DirtiesContext
public
class
SampleHateoasApplicationTests
{
@Value
(
"${local.server.port}"
)
private
int
port
;
@Test
public
void
hasHalLinks
()
throws
Exception
{
ResponseEntity
<
String
>
entity
=
new
TestRestTemplate
().
getForEntity
(
"http://localhost:"
+
this
.
port
+
"/customers/1"
,
String
.
class
);
assertThat
(
entity
.
getStatusCode
(),
equalTo
(
HttpStatus
.
OK
));
assertThat
(
entity
.
getBody
(),
startsWith
(
"{\"id\":1,\"firstName\":\"Oliver\""
+
",\"lastName\":\"Gierke\""
));
assertThat
(
entity
.
getBody
(),
containsString
(
"_links\":{\"self\":{\"href\""
));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment