邮递员中不支持的媒体类型

Unsupported Media Type in postman

我正在使用 oauth2 和 jwt 实现 spring 安全性。 下面是我的登录函数

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

但是当我用 postman 尝试 post 请求时,它显示

{
  "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) 时它会打印令牌?我无法弄清楚什么是 wrong.Any 帮助表示赞赏。

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

转到 POST 请求中的正文,在那里您会找到原始选项。

在它旁边,会有一个下拉,select JSON (application.json).

Http 415 Media Unsupported 仅当应用程序不支持您提供的内容类型 header 时才会响应。

使用 POSTMAN,您发送的 Content-type header 是 Content type 'multipart/form-data 而不是 application/json。在 ajax 代码中,您将其正确设置为 application/json。在 POSTMAN 中传递正确的 Content-type header 即可。

我也遇到了这个错误。我在更改为 XML(text/xml) 后在正文中使用了文本,得到了预期的结果。

  • 如果您的请求是XML请求使用XML(text/xml).

  • 如果您的请求是JSON请求使用JSON(application/json)

当我在 XML 发生这种情况时;
我刚刚将“application/XML”更改为“text/XML”,
这解决了我的问题。

如果您在邮递员中仍然因不支持的媒体类型而失败 调用 SOAP 端点时,您可以尝试:

内容类型:application/soap+xml

我遇到了这个问题。我在身份验证选项卡上进行了身份验证,设置为在正文中传递凭据。

当我将 Body 设置为 None 时,我发生了这个错误。

所以我需要在 postman 中有一个空主体,设置为原始 JSON 以允许它工作,即使我的主要请求是查询字符串中的参数。

{
}