0

I'm stuck with a problem on a built Spring Native image:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [collection type; class java.util.HashSet, contains [simple type, class java.lang.Object]]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of java.util.HashSet (no Creators, like default constructor, exist): no default no-arguments constructor found at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 5, column: 14] (through reference chain: com.example.app.payload.request.SignupRequest["roles"])] with root cause

From this description it seems that I need use custom deserializer, but the problem appears only if I run code from native image - same code works perfectly well if run from JAR.

POJOs are very simple:

public class SignupRequest {
    @NotBlank
    @Size(min = 3, max = 20)
    private String username;

    @NotBlank
    @Size(max = 50)
    @Email
    private String email;

    private Set<String> roles;

    @NotBlank
    @Size(min = 6, max = 40)
    private String password;

    // getters & setters no Lombok (but Lombok is in project)
}

Controller uses standard (nothing fancy) annotations:

public ResponseEntity<MessageResponse> registerUser(@Valid @RequestBody SignupRequest signUpRequest)

Has anyone encountered a similar problem?

Garion S.
  • 119
  • 2
  • 9

1 Answers1

0

Finally I found missing part - I have to add HashSet to SerializationHint:

@SpringBootApplication
@SerializationHint(types = {
    java.util.HashSet.class
})
public class SpringNativeApplication {
    public static void main(String[] args) {
      // ...
    }
}
Garion S.
  • 119
  • 2
  • 9