Skip to content

Update documentation to show how to create an ALPS serving controller for profile purposes. #1038

Closed
@gregturn

Description

@gregturn

Problem:

Create a web controller method that returns Alps. It will typically produce either application/json or application/hal+json. Change the produces clause to ALPS_JSON_VALUE, and things fall apart.

@GetMapping(value = "/hypermedia/items/profile", produces = MediaTypes.ALPS_JSON_VALUE)
public Alps profile() {
	return alps() //
			.descriptor(Collections.singletonList(descriptor() //
					.id(Item.class.getSimpleName() + "-representation") //
					.descriptor(Arrays.stream(Item.class.getDeclaredFields()) //
							.map(field -> descriptor() //
									.name(field.getName()) //
									.type(Type.SEMANTIC) //
									.build()) //
							.collect(Collectors.toList())) //
					.build())) //
			.build();
}

A Spring REST Docs test method like this...

@Test
void findProfile() {
	this.webTestClient.get().uri("/hypermedia/items/profile") //
			.accept(MediaTypes.ALPS_JSON) //
			.exchange() //
			.expectStatus().isOk() //
			.expectBody() //
			.consumeWith(document("profile", //
					preprocessResponse(prettyPrint())));
}

...will produce this type of stack error:

org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/json' not supported for bodyType=java.util.LinkedHashMap<?, ?>
	at org.springframework.web.reactive.function.BodyInserters.unsupportedError(BodyInserters.java:368) ~[spring-webflux-5.2.0.BUILD-20190802.092224-620.jar:5.2.0.BUILD-SNAPSHOT]
	at org.springframework.web.reactive.function.BodyInserters.lambda$writeWithMessageWriters$11(BodyInserters.java:358) ~[spring-webflux-5.2.0.BUILD-20190802.092224-620.jar:5.2.0.BUILD-SNAPSHOT]
	at java.util.Optional.orElseGet(Optional.java:267) ~[na:1.8.0_212]

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions