邮递员中不支持的媒体类型 [英] Unsupported Media Type in postman

查看:85
本文介绍了邮递员中不支持的媒体类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用oauth2和jwt实现spring security. 以下是我的登录功能

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';
        }
    });
}

下面我有控制器

 @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"
}

但是当我在ajax调用中执行cosole.log(data)时,它会打印令牌?

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.

推荐答案

您需要在邮递员中将content-type设置为JSON(application/json).

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

转到POST请求中的正文,您将在其中找到原始选项.

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

在它旁边,将出现一个下拉列表,选择JSON(application.json).

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

这篇关于邮递员中不支持的媒体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆