【发布时间】:2016-10-17 03:58:00
【问题描述】:
我必须将 json 发布到 api_url 以进行登录。
{
"username":"testre","password":"password"
}
当我使用邮递员检查这个 api 时,它会回复成功的身份验证,如下所示。
{
"status": "success",
"code": 200,
"message": "username, password validated.",
"data": [
{
"password": "password",
"username": "testre"
}
],
"links": [
{
"rel": "self",
"link": "http://localhost:2222/pizza-shefu/api/v1.0/customers/login/"
},
{
"rel": "profile",
"link": "http://localhost:2222/pizza-shefu/api/v1.0/customers/testre"
}
]
}
对于如下所示的未经授权的 json。
{
"status": "unauthorized",
"code": 401,
"errorMessage": "HTTP_UNAUTHORIZED",
"description": "credentials provided are not authorized."
}
之前我使用 java 编写代码来检索它。但现在我想在春季使用 RestTemplate 对其进行重构。问题是我阅读的每个示例都是针对固定数量的变量https://spring.io/guides/gs/consuming-rest/ 编写的。在这里,我根据登录成功状态得到不同数量的变量。我是spring的新手,所以我在创建从rest模板获得的登录回复类时很困惑。 (例如示例中的这个Quote quote = restTemplate.getForObject("http://gturnquist-quoters.cfapps.io/api/random", Quote.class); 但我需要返回一个json对象)。我不知道如何编写 RestTemplate 部分。
【问题讨论】:
-
只需添加字段超集,并将它们设为可选。如果需要,您可以将
status和code设为必需,但其余部分是可选的,或者取决于状态代码。其余部分由 Java 代码处理。
标签: java json spring spring-mvc