从请求标头Spring Boot读取POJO

时间:2018-08-24 07:50:38

标签: java spring spring-boot

我正在尝试解析从客户端到我的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 / verify”

因此,我尝试以某种方式构建自己的转换器。下面是我的授权POJO和AuthorizationEditor Converter

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

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

0 个答案:

没有答案