Skip to content

Commit 8c366e1

Browse files
gregturnodrotbohm
authored andcommitted
#833 - Add API to register custom media types.
1 parent 8af414d commit 8c366e1

19 files changed

+706
-418
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2019 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+
package org.springframework.hateoas.collectionjson;
17+
18+
import java.util.List;
19+
20+
import org.springframework.context.annotation.Bean;
21+
import org.springframework.context.annotation.Configuration;
22+
import org.springframework.hateoas.LinkDiscoverer;
23+
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
24+
import org.springframework.hateoas.config.Hypermedia;
25+
import org.springframework.http.MediaType;
26+
27+
import com.fasterxml.jackson.databind.DeserializationFeature;
28+
import com.fasterxml.jackson.databind.ObjectMapper;
29+
30+
/**
31+
* @author Greg Turnquist
32+
*/
33+
@Configuration
34+
public class CollectionJsonConfigurer {
35+
36+
@Bean
37+
Hypermedia collectionJsonBean() {
38+
39+
return new Hypermedia() {
40+
@Override
41+
public List<MediaType> getMediaTypes() {
42+
return HypermediaType.COLLECTION_JSON.getMediaTypes();
43+
}
44+
45+
@Override
46+
public ObjectMapper createObjectMapper(ObjectMapper mapper) {
47+
48+
ObjectMapper mapper1 = mapper.copy();
49+
50+
mapper1.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
51+
mapper1.registerModule(new Jackson2CollectionJsonModule());
52+
53+
return mapper1;
54+
}
55+
};
56+
}
57+
58+
@Bean
59+
LinkDiscoverer linkDiscoverer() {
60+
return new CollectionJsonLinkDiscoverer();
61+
}
62+
63+
}

src/main/java/org/springframework/hateoas/config/EnableHypermediaSupport.java

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2018 the original author or authors.
2+
* Copyright 2013-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,24 +20,23 @@
2020
import java.lang.annotation.Retention;
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
23+
import java.util.Arrays;
24+
import java.util.List;
25+
import java.util.Optional;
2326

2427
import org.springframework.context.ApplicationContext;
2528
import org.springframework.context.annotation.Import;
26-
import org.springframework.hateoas.EntityLinks;
27-
import org.springframework.hateoas.LinkDiscoverer;
29+
import org.springframework.hateoas.MediaTypes;
30+
import org.springframework.hateoas.collectionjson.CollectionJsonConfigurer;
31+
import org.springframework.hateoas.hal.HalConfigurer;
32+
import org.springframework.hateoas.hal.forms.HalFormsConfigurer;
33+
import org.springframework.hateoas.uber.UberConfigurer;
34+
import org.springframework.http.MediaType;
2835

2936
/**
30-
* Activates hypermedia support in the {@link ApplicationContext}. Will register infrastructure beans available for
31-
* injection to ease building hypermedia related code. Which components get registered depends on the hypermedia type
32-
* being activated through the {@link #type()} attribute. Hypermedia-type-specific implementations of the following
33-
* components will be registered:
34-
* <ul>
35-
* <li>{@link LinkDiscoverer}</li>
36-
* <li>a Jackson 2 module to correctly marshal the resource model classes into the appropriate representation.
37-
* </ul>
37+
* Activates hypermedia support in the {@link ApplicationContext}. Will register infrastructure beans to support all
38+
* appropriate web stacks based on selected {@link Hypermedia}-type as well as the classpath.
3839
*
39-
* @see LinkDiscoverer
40-
* @see EntityLinks
4140
* @author Oliver Gierke
4241
* @author Greg Turnquist
4342
*/
@@ -61,35 +60,55 @@
6160
* @author Oliver Gierke
6261
* @author Greg Turnquist
6362
*/
64-
enum HypermediaType {
63+
enum HypermediaType implements Hypermedia {
6564

6665
/**
6766
* HAL - Hypermedia Application Language.
6867
*
6968
* @see http://stateless.co/hal_specification.html
7069
* @see http://tools.ietf.org/html/draft-kelly-json-hal-05
7170
*/
72-
HAL,
71+
HAL(HalConfigurer.class, MediaTypes.HAL_JSON, MediaTypes.HAL_JSON_UTF8),
7372

7473
/**
7574
* HAL-FORMS - Independent, backward-compatible extension of the HAL designed to add runtime FORM support
7675
*
7776
* @see https://rwcbook.github.io/hal-forms/
7877
*/
79-
HAL_FORMS,
78+
HAL_FORMS(HalFormsConfigurer.class, MediaTypes.HAL_FORMS_JSON),
8079

8180
/**
8281
* Collection+JSON
8382
*
8483
* @see http://amundsen.com/media-types/collection/format/
8584
*/
86-
COLLECTION_JSON,
85+
COLLECTION_JSON(CollectionJsonConfigurer.class, MediaTypes.COLLECTION_JSON),
8786

8887
/**
8988
* UBER Hypermedia
9089
*
9190
* @see http://uberhypermedia.org/
9291
*/
93-
UBER;
92+
UBER(UberConfigurer.class, MediaTypes.UBER_JSON);
93+
94+
private final Class<?> configurer;
95+
private final List<MediaType> mediaTypes;
96+
97+
HypermediaType(Class<?> configurer, MediaType... mediaTypes) {
98+
99+
this.configurer = configurer;
100+
this.mediaTypes = Arrays.asList(mediaTypes);
101+
}
102+
103+
@Override
104+
public List<MediaType> getMediaTypes() {
105+
return this.mediaTypes;
106+
}
107+
108+
@Override
109+
public Optional<Class<?>> configurer() {
110+
return Optional.ofNullable(this.configurer);
111+
}
112+
94113
}
95114
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2019 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+
package org.springframework.hateoas.config;
17+
18+
import java.util.List;
19+
import java.util.Optional;
20+
21+
import org.springframework.http.MediaType;
22+
import org.springframework.util.MimeType;
23+
24+
import com.fasterxml.jackson.databind.ObjectMapper;
25+
26+
/**
27+
* Interface for registering custom hypermedia handlers.
28+
*
29+
* @author Greg Turnquist
30+
*/
31+
public interface Hypermedia {
32+
33+
/**
34+
* {@link MediaType}s this hypermedia can handle.
35+
*/
36+
List<MediaType> getMediaTypes();
37+
38+
/**
39+
* Convert list of {@link MediaType}s into an array of {@link MimeType}s to support various Spring constructors.
40+
*/
41+
default MimeType[] getMimeTypes() {
42+
return getMediaTypes().toArray(new MimeType[0]);
43+
}
44+
45+
/**
46+
* Create an {@link ObjectMapper} and register custom serializers and deserializers for the supported media types.
47+
*/
48+
default ObjectMapper createObjectMapper(ObjectMapper mapper) {
49+
return mapper.copy();
50+
}
51+
52+
default Optional<Class<?>> configurer() {
53+
return Optional.empty();
54+
}
55+
}

src/main/java/org/springframework/hateoas/config/HypermediaObjectMapperCreator.java

Lines changed: 0 additions & 120 deletions
This file was deleted.

0 commit comments

Comments
 (0)