【发布时间】:2018-10-12 07:47:49
【问题描述】:
我正在从 Spring Boot 控制器 (1.5.16.RELEASE) 返回 org.json JSONObject。我得到了一个额外的地图对象。
我的代码-
@GetMapping(value = Constants.API_LOGIN)
public Object login(@RequestParam String userName, @RequestParam String password) throws JSONException {
UserAuth userAuth = new UserAuth();
UserAuth user = null;
try {
Preconditions.checkArgument(!Strings.isNullOrEmpty(userName), "empty UserName");
Preconditions.checkArgument(!Strings.isNullOrEmpty(password), "empty password");
userAuth.setUserName(userName);
userAuth.setPassword(password);
user = authService.checkAuth(userAuth);
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
JSONObject json = new JSONObject();
if (user != null) {
json.put("status", true);
json.put("message", "login success");
return ResponseEntity.status(HttpStatus.OK).body(json);
} else {
json.put("status", false);
json.put("message", "username or password doesnt match");
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(json);
}
}
pom.xml
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
预期的 Json
{
"message": "login success",
"status": true
}
我正在获取 JSON
{
"map": {
"message": "login success",
"status": true
}
}
我不知道为什么我的 JSONObject 中有额外的地图对象。
【问题讨论】:
-
你能否展示你完整的控制器方法..你在哪里返回 Json 对象
-
添加了完整的控制器方法@kj007
标签: java spring-boot org.json