Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

spring - Unsupported Media Type in postman

I am implementing spring security with oauth2 and jwt. the below is my login function

function doLogin(loginData) {

    $.ajax({
        url :  back+"/auth/secret",
        type : "POST",
        data : JSON.stringify(loginData),
        contentType : "application/json; charset=utf-8",
        dataType : "json",
        async : false,
        success : function(data, textStatus, jqXHR) {

            setJwtToken(data.token);


        },
        error : function(jqXHR, textStatus, errorThrown) {
            alert("an unexpected error occured: " + errorThrown);
            window.location.href= back+'/login_page.html';
        }
    });
}

And down I have the Controller

 @RequestMapping(value = "auth/secret", method = RequestMethod.POST)
    public ResponseEntity<?> createAuthenticationToken(@RequestBody JwtAuthenticationRequest authenticationRequest, Device device) throws AuthenticationException {
        System.out.println();
        logger.info("authentication request : " + authenticationRequest.getUsername() + " " + authenticationRequest.getPassword());
        // Perform the security
         System.out.println( authenticationRequest.getUsername()+"is the username and "+authenticationRequest.getPassword());
        final Authentication authentication = authenticationManager.authenticate(
                new UsernamePasswordAuthenticationToken(
                        authenticationRequest.getUsername(),
                        authenticationRequest.getPassword()


                        )


        );
        SecurityContextHolder.getContext().setAuthentication(authentication);

        logger.info("authentication passed");

        // Reload password post-security so we can generate token
        final UserDetails userDetails = userDetailsService.loadUserByUsername(authenticationRequest.getUsername());
        final String token = jwtTokenUtil.generateToken(userDetails, device);
        logger.info("token " + token);

        // Return the token
        return ResponseEntity.ok(new JwtAuthenticationResponse(token));
    }

But when I try the post request with the postman it shows me

{
  "timestamp": 1488973010828,
  "status": 415,
  "error": "Unsupported Media Type",
  "exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
  "message": "Content type 'multipart/form-data;boundary=----WebKitFormBoundaryY4KgeeQ9ONtKpvkQ;charset=UTF-8' not supported",
  "path": "/TaxiVis/auth/secret"
}

But when I do cosole.log(data) in the ajax call it prints the token?I could not figure out what is wrong.Any help is appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to set the content-type in postman as JSON (application/json).

Go to the body inside your POST request, there you will find the raw option.

Right next to it, there will be a drop down, select JSON (application.json).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...