0

I use an external library that has a method:

public class CustomUserDetails implements UserDetails {

    private final User principal;
    private final Collection<GrantedAuthority> authorities;

    //constructor, getters, setters

    @Override
    public String getPassword() {
        throw new UnsupportedOperationException("Not allowed");
    }
}

And I have a @RequestMapping that returns an instance of that object:

@RequestMapping(value = "/authorities", method = GET)
    public ResponseEntity getAuthorities() {
        return new ResponseEntity<>(authenticatedUser.getAuthentication(), HttpStatus.OK); //getAuthentication returns CustomUserDetails instance
    }

And I'm getting org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: Not allowed.

How can I handle this? Ingore or set some default value to this property.

UPD I cannot add @JsonIngore to that class because it's not my library, I don't have access to its source.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Frankie Drake
  • 1,338
  • 9
  • 24
  • 40

1 Answers1

0

You can write JsonIgnore annotation on top of getPassword() method.This way you tell Jackon to not attempt serialization of that field

https://fasterxml.github.io/jackson-annotations/javadoc/2.4/com/fasterxml/jackson/annotation/JsonIgnore.html

  • Please, read my update. I cannot add @JsonIngore to that class because it's not my library, I don't have access to its source. – Frankie Drake Jul 09 '17 at 18:10
  • You can make a custom clone class and write it there and serialize that one. Not efficient, but effective – Araymer Jul 09 '17 at 18:12