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
74317a22
Commit
74317a22
authored
Oct 30, 2015
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '1.2.x'
parents
00e207dc
1f923605
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
4 deletions
+22
-4
CustomerController.java
.../src/main/java/sample/hateoas/web/CustomerController.java
+3
-2
SampleHateoasApplicationTests.java
...st/java/sample/hateoas/SampleHateoasApplicationTests.java
+19
-2
No files found.
spring-boot-samples/spring-boot-sample-hateoas/src/main/java/sample/hateoas/web/CustomerController.java
View file @
74317a22
...
@@ -26,6 +26,7 @@ import org.springframework.hateoas.Resource;
...
@@ -26,6 +26,7 @@ import org.springframework.hateoas.Resource;
import
org.springframework.hateoas.Resources
;
import
org.springframework.hateoas.Resources
;
import
org.springframework.http.HttpEntity
;
import
org.springframework.http.HttpEntity
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.PathVariable
;
...
@@ -47,7 +48,7 @@ public class CustomerController {
...
@@ -47,7 +48,7 @@ public class CustomerController {
this
.
entityLinks
=
entityLinks
;
this
.
entityLinks
=
entityLinks
;
}
}
@RequestMapping
(
method
=
RequestMethod
.
GET
)
@RequestMapping
(
method
=
RequestMethod
.
GET
,
produces
=
MediaType
.
APPLICATION_JSON_VALUE
)
HttpEntity
<
Resources
<
Customer
>>
showCustomers
()
{
HttpEntity
<
Resources
<
Customer
>>
showCustomers
()
{
Resources
<
Customer
>
resources
=
new
Resources
<
Customer
>(
Resources
<
Customer
>
resources
=
new
Resources
<
Customer
>(
this
.
repository
.
findAll
());
this
.
repository
.
findAll
());
...
@@ -55,7 +56,7 @@ public class CustomerController {
...
@@ -55,7 +56,7 @@ public class CustomerController {
return
new
ResponseEntity
<
Resources
<
Customer
>>(
resources
,
HttpStatus
.
OK
);
return
new
ResponseEntity
<
Resources
<
Customer
>>(
resources
,
HttpStatus
.
OK
);
}
}
@RequestMapping
(
value
=
"/{id}"
,
method
=
RequestMethod
.
GET
)
@RequestMapping
(
value
=
"/{id}"
,
method
=
RequestMethod
.
GET
,
produces
=
MediaType
.
APPLICATION_JSON_VALUE
)
HttpEntity
<
Resource
<
Customer
>>
showCustomer
(
@PathVariable
Long
id
)
{
HttpEntity
<
Resource
<
Customer
>>
showCustomer
(
@PathVariable
Long
id
)
{
Resource
<
Customer
>
resource
=
new
Resource
<
Customer
>(
this
.
repository
.
findOne
(
id
));
Resource
<
Customer
>
resource
=
new
Resource
<
Customer
>(
this
.
repository
.
findOne
(
id
));
resource
.
add
(
this
.
entityLinks
.
linkToSingleResource
(
Customer
.
class
,
id
));
resource
.
add
(
this
.
entityLinks
.
linkToSingleResource
(
Customer
.
class
,
id
));
...
...
spring-boot-samples/spring-boot-sample-hateoas/src/test/java/sample/hateoas/SampleHateoasApplicationTests.java
View file @
74317a22
...
@@ -16,6 +16,8 @@
...
@@ -16,6 +16,8 @@
package
sample
.
hateoas
;
package
sample
.
hateoas
;
import
java.net.URI
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.junit.runner.RunWith
;
...
@@ -23,9 +25,12 @@ import org.springframework.beans.factory.annotation.Value;
...
@@ -23,9 +25,12 @@ import org.springframework.beans.factory.annotation.Value;
import
org.springframework.boot.test.SpringApplicationConfiguration
;
import
org.springframework.boot.test.SpringApplicationConfiguration
;
import
org.springframework.boot.test.TestRestTemplate
;
import
org.springframework.boot.test.TestRestTemplate
;
import
org.springframework.boot.test.WebIntegrationTest
;
import
org.springframework.boot.test.WebIntegrationTest
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.RequestEntity
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.test.annotation.DirtiesContext
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
static
org
.
hamcrest
.
Matchers
.
containsString
;
import
static
org
.
hamcrest
.
Matchers
.
containsString
;
...
@@ -36,7 +41,6 @@ import static org.junit.Assert.assertThat;
...
@@ -36,7 +41,6 @@ import static org.junit.Assert.assertThat;
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@SpringApplicationConfiguration
(
SampleHateoasApplication
.
class
)
@SpringApplicationConfiguration
(
SampleHateoasApplication
.
class
)
@WebIntegrationTest
(
randomPort
=
true
)
@WebIntegrationTest
(
randomPort
=
true
)
@DirtiesContext
public
class
SampleHateoasApplicationTests
{
public
class
SampleHateoasApplicationTests
{
@Value
(
"${local.server.port}"
)
@Value
(
"${local.server.port}"
)
...
@@ -52,4 +56,17 @@ public class SampleHateoasApplicationTests {
...
@@ -52,4 +56,17 @@ public class SampleHateoasApplicationTests {
assertThat
(
entity
.
getBody
(),
containsString
(
"_links\":{\"self\":{\"href\""
));
assertThat
(
entity
.
getBody
(),
containsString
(
"_links\":{\"self\":{\"href\""
));
}
}
@Test
public
void
producesJsonWhenXmlIsPreferred
()
throws
Exception
{
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
set
(
HttpHeaders
.
ACCEPT
,
"application/xml;q=0.9,application/json;q=0.8"
);
RequestEntity
<?>
request
=
new
RequestEntity
<
Void
>(
headers
,
HttpMethod
.
GET
,
URI
.
create
(
"http://localhost:"
+
this
.
port
+
"/customers/1"
));
ResponseEntity
<
String
>
response
=
new
TestRestTemplate
().
exchange
(
request
,
String
.
class
);
assertThat
(
response
.
getStatusCode
(),
equalTo
(
HttpStatus
.
OK
));
assertThat
(
response
.
getHeaders
().
getContentType
(),
equalTo
(
MediaType
.
parseMediaType
(
"application/json;charset=UTF-8"
)));
}
}
}
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