java - 从请求标头 Spring Boot 中读取 POJO
问题描述
我正在尝试解析从客户端发送到我的 Spring Boot 服务器端的请求标头中的对象。
@CrossOrigin(origins = "http://localhost")
@RequestMapping(method = RequestMethod.GET, value = "/verify")
public ResponseEntity<genericResponse> verifyToken(@RequestHeader("Authorization") Authorization header) {
System.out.println(header.getAccess() + " - " + header.getTimeStamp() + " - ");
}
但我不断收到此错误
无法将类型“java.lang.String”的值转换为所需类型“com.bus.api.dto.Requests.Authorization”;嵌套异常是 java.lang.IllegalStateException:无法将类型“java.lang.String”的值转换为所需类型“com.bus.api.dto.Requests.Authorization”:找不到匹配的编辑器或转换策略“,路径” :“/v1/clients/验证”
所以我尝试以某种方式构建自己的转换器。下面是我的授权 POJO 和 AuthorizationEditor 转换器
public class Authorization {
private String token;
private String timeStamp;
private String access;
// ... setters and getters are removed for brevity
}
编辑
public class AuthorizationEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
System.out.println(text);
JSONObject obj = new JSONObject(text);
Authorization authorization = new Authorization();
authorization.setAccess(obj.getString("token"));
authorization.setTimeStamp(obj.getString("timeStamp"));
authorization.setAccess(obj.getString("access"));
setValue(authorization);
}
}
给我文本 [Object Object]然后prinln
给出错误
JSONObject 文本必须在 1 [字符 2 第 1 行] 处以 '{' 开头
我通过ajax的客户端请求
function Auth() {
if (token != null && profile != null) {
var HEADER = {
token: token,
timeStamp: new Date().getTime(),
access: profile
}
console.log(JSON.stringify(HEADER));
/**
* This request would first request a token from the Auth0 Server
* The token is returned and that token is used to access API resource
*/
network.call(constants.VERIFY_URL, constants.TYPE_GET, {}, JSON.stringify(HEADER).trim());
// window.location.href = "dashboard.html";
} else {
webAuth.authorize();
}
}
解决方案
推荐阅读
- ios - 如何将 UILabel 与 UITableViewCell 的 imageView 中的 SF Symbols 和自定义图像对齐?
- python - 彩票插入排序算法
- kotlin - 有没有办法减少/删除 Kotlin 中的冗余泛型参数?
- angular - 如何正确实现 Angular Guard?
- knockout.js - Knockout DataTables Row Click After fetch data
- python - AttributeError:'str' 对象在 for 循环中没有属性 'tk'
- java - 大括号正确匹配时应为“{”
- mysql - 如何通过 xhr fetch response 防止网络请求中显示个人信息
- excel - 如果我存储了一个 IRibbonUI 对象,我可以使用它使用 VBA 代码将控件添加到功能区吗?
- java - MS Graph Java SDK 引发身份验证异常