spring-projects / spring-projects/spring-hateoas

Unable to marshal Resources - HAL should be used

Open
#692 14 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.1k
Forks
476
PR merge metrics
No merged PRs in 30d

Description

Invalid content type is used, for Resources endpoint in HAL enabled application.

This requires special code @GetMapping(produces = {MediaType.APPLICATION_JSON_VALUE, "application/hal+json"}) for endpoint method returning Resources.

Logged error:

.w.s.m.s.DefaultHandlerExceptionResolver : Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not marshal [Resources { content: [FooApplication.FooResource(foo=foo)], links: [] }]: null; nested exception is javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "com.foo.FooApplication$FooResource" as an element because it is missing an @XmlRootElement annotation]

Sample application:

@SpringBootApplication
public class FooApplication {

	public static void main(String[] args) {
		SpringApplication.run(FooApplication.class, args);
	}

	@RestController("/foos")
	public class FooController {

		@GetMapping
		public Resources<FooResource> foo() {
			List<FooResource> foos = Stream.of(new FooResource("foo")).collect(Collectors.toList());
			return new Resources(foos);
		}
	}

	@Data
	public class FooResource extends ResourceSupport {
		private final String foo;
	}

}

Dependencies:

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-hateoas</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
	</dependencies>

This is similar to bug: https://github.com/spring-guides/tut-bookmarks/issues/8

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the FooController.foo entry point and its @GetMapping in the supplied sample application, then trace how the Resources return value is selected for writing. Reproduce the XML marshalling error with the listed dependencies and verify that the endpoint produces HAL without requiring the special produces declaration.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend-api-design
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.