Skip to content

Commit 6ad7257

Browse files
royclarksonDave Syer
authored andcommitted
Add auto-configuration for @EnableHypermediaSupport
If Spring HATEOAS is on the class path, then @EnableHypermediaSupport will be auto-configured with HAL support.
1 parent e8e59ea commit 6ad7257

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

spring-boot-autoconfigure/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@
136136
<artifactId>spring-data-redis</artifactId>
137137
<optional>true</optional>
138138
</dependency>
139+
<dependency>
140+
<groupId>org.springframework.hateoas</groupId>
141+
<artifactId>spring-hateoas</artifactId>
142+
<optional>true</optional>
143+
</dependency>
139144
<dependency>
140145
<groupId>com.lambdaworks</groupId>
141146
<artifactId>lettuce</artifactId>
@@ -229,5 +234,10 @@
229234
<artifactId>slf4j-jdk14</artifactId>
230235
<scope>test</scope>
231236
</dependency>
237+
<dependency>
238+
<groupId>org.springframework.plugin</groupId>
239+
<artifactId>spring-plugin-core</artifactId>
240+
<scope>test</scope>
241+
</dependency>
232242
</dependencies>
233243
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 org.springframework.boot.autoconfigure.hateoas;
18+
19+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
20+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
21+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
23+
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.hateoas.Resource;
26+
import org.springframework.hateoas.config.EnableHypermediaSupport;
27+
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
28+
29+
/**
30+
* {@link EnableAutoConfiguration Auto-configuration} for Spring HATEOAS's
31+
* {@link EnableHypermediaSupport}.
32+
*
33+
* @author Roy Clarkson
34+
*/
35+
@Configuration
36+
@ConditionalOnClass(Resource.class)
37+
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
38+
public class HypermediaAutoConfiguration {
39+
40+
@Configuration
41+
@EnableHypermediaSupport(type = HypermediaType.HAL)
42+
@ConditionalOnWebApplication
43+
protected static class HypermediaConfiguration {
44+
45+
}
46+
47+
}

spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration,\
1111
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
1212
org.springframework.boot.autoconfigure.data.JpaRepositoriesAutoConfiguration,\
1313
org.springframework.boot.autoconfigure.data.MongoRepositoriesAutoConfiguration,\
14+
org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\
1415
org.springframework.boot.autoconfigure.redis.RedisAutoConfiguration,\
1516
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
1617
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 org.springframework.boot.autoconfigure.hateoas;
18+
19+
import static org.junit.Assert.assertNotNull;
20+
import static org.junit.Assert.assertTrue;
21+
22+
import org.junit.After;
23+
import org.junit.Test;
24+
import org.springframework.hateoas.LinkDiscoverer;
25+
import org.springframework.hateoas.LinkDiscoverers;
26+
import org.springframework.hateoas.MediaTypes;
27+
import org.springframework.hateoas.hal.HalLinkDiscoverer;
28+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
29+
30+
/**
31+
* Tests for {@link HypermediaAutoConfiguration}.
32+
*
33+
* @author Roy Clarkson
34+
*/
35+
public class HypermediaAutoConfigurationTests {
36+
37+
private AnnotationConfigWebApplicationContext context;
38+
39+
@After
40+
public void close() {
41+
if (this.context != null) {
42+
this.context.close();
43+
}
44+
}
45+
46+
@Test
47+
public void linkDiscoverersCreated() throws Exception {
48+
this.context = new AnnotationConfigWebApplicationContext();
49+
this.context.register(HypermediaAutoConfiguration.class);
50+
this.context.refresh();
51+
LinkDiscoverers discoverers = this.context.getBean(LinkDiscoverers.class);
52+
assertNotNull(discoverers);
53+
LinkDiscoverer discoverer = discoverers.getLinkDiscovererFor(MediaTypes.HAL_JSON);
54+
assertTrue(HalLinkDiscoverer.class.isInstance(discoverer));
55+
}
56+
57+
}

spring-boot-dependencies/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<spring-data-redis.version>1.1.1.RELEASE</spring-data-redis.version>
8989
<spring-data-rest.version>2.0.2.RELEASE</spring-data-rest.version>
9090
<spring-hateoas.version>0.9.0.RELEASE</spring-hateoas.version>
91+
<spring-plugin.version>1.0.0.RELEASE</spring-plugin.version>
9192
<spring-rabbit.version>1.2.2.RELEASE</spring-rabbit.version>
9293
<spring-mobile.version>1.1.1.RELEASE</spring-mobile.version>
9394
<spring-security.version>3.2.3.RELEASE</spring-security.version>
@@ -542,6 +543,11 @@
542543
<artifactId>spring-hateoas</artifactId>
543544
<version>${spring-hateoas.version}</version>
544545
</dependency>
546+
<dependency>
547+
<groupId>org.springframework.plugin</groupId>
548+
<artifactId>spring-plugin-core</artifactId>
549+
<version>${spring-plugin.version}</version>
550+
</dependency>
545551
<dependency>
546552
<groupId>org.springframework.data</groupId>
547553
<artifactId>spring-data-rest-webmvc</artifactId>

0 commit comments

Comments
 (0)