#Cannot deserialize value of type Error

13 messages · Page 1 of 1 (latest)

quasi knotBOT
#

This post has been reserved for your question.

Hey @silk sierra! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

silk sierra
#
    login(user, password) {
        //TODO: inloggen met POST
        let data = {
            'username': user,
            'password': password
        }
        console.log(data);
        return fetch("/restservices/authentication", {
            method: "POST",
            body: data,
            headers: {
                'Content-Type': 'application/json'
            }
        }).then(function (response) {
            if (response.ok) return response.json();
                else throw "Wrong username/password"
        }).then((myJson) => {
            window.localStorage.setItem("myJWT", myJson.JWT);
        })
    }
#
document.forms.login.addEventListener('submit', e => {
    e.preventDefault();
    service.login(document.forms.login.username.value, document.forms.login.password.value).then(() => {
        window.location.reload();
    })

});
#

upper function is getting called by the function below

#

@Path("authentication")
public class AuthenticationResource {
    public static final Key key = MacProvider.generateKey();

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response authenticateUser(LogonRequest logonRequest) {
        System.out.println(logonRequest.username + " " + logonRequest.password);
        try {
            String role = MyUser.validateLogin(logonRequest.username, logonRequest.password);
            if (role==null) throw new IllegalAccessException("validation failed");
            String token = createToken(logonRequest.username, role);
            return Response.ok(Map.of("JWT", token)).build();
        }catch (IllegalAccessException e) {
            e.printStackTrace();

        }
        return Response.status(Response.Status.UNAUTHORIZED).build();

    }

    private String createToken(String username, String role) throws JwtException {
        Calendar expiration = Calendar.getInstance();
        expiration.add(Calendar.MINUTE, 30);


        return Jwts.builder()
                .setSubject(username)
                .setExpiration(expiration.getTime())
                .claim("role", role)
                .signWith(SignatureAlgorithm.HS512, key)
                .compact();

    }

    private static class LogonRequest{
        public String username;
        public String password;
    }


}
#

this is my athentionresource

#

and i get this error

#

Cannot deserialize value of type nl.hu.bep.setup.security.AuthenticationResource$LogonRequest from Array value (token JsonToken.START_ARRAY)
at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 1, column: 1]

#

this is the preview/error when i track it in my console.log

#

but idk what im doing wrong ;0

#

the json im sending