spring 邮递员中不支持的媒体类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42670413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unsupported Media Type in postman
提问by Hades
I am implementing spring security with oauth2 and jwt. the below is my login function
我正在使用 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';
}
});
}
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.
但是,当我cosole.log(data)在 ajax 调用中执行时 ,它会打印令牌?我不知道出了什么问题。感谢任何帮助。
回答by Hades
You need to set the content-typein postman as JSON (application/json).
您需要将content-type邮递员设置为 JSON (application/json)。
Go to the body inside your POST request, there you will find the raw option.
转到 POST 请求中的正文,您会在那里找到原始选项。
Right next to it, there will be a drop down, select JSON (application.json).
在它旁边,会有一个下拉菜单,选择 JSON (application.json)。
回答by ritesh.garg
Http 415 Media Unsupportedis responded back only when the content type header you are providing is not supported by the application.
Http 415 Media Unsupported仅当应用程序不支持您提供的内容类型标头时才会响应。
With POSTMAN, the Content-typeheader you are sending is Content type 'multipart/form-datanot application/json. While in the ajax code you are setting it correctly to application/json. Pass the correct Content-type header in POSTMAN and it will work.
使用 POSTMAN,Content-type您发送的标头Content type 'multipart/form-data不是application/json. 在 ajax 代码中,您将其正确设置为application/json. 在 POSTMAN 中传递正确的 Content-type 标头,它将起作用。
回答by Mahesh Chaudhary
I also got this error .I was using Text inside body after changing to XML(text/xml) , got result as expected.
我也遇到了这个错误。我在更改为 XML(text/xml) 后在 body 内使用 Text ,得到了预期的结果。
If your request is XML Request use XML(test/xml).
If your request is JSON Request use JSON(application/json)
如果您的请求是 XML 请求,请使用 XML(test/xml)。
如果您的请求是 JSON 请求,请使用 JSON(application/json)

