[英]org.json JSONObject adding extra object to JSONObject while returning it from spring boot controller
I am returning org.json JSONObject from spring boot controller (1.5.16.RELEASE). 我正在从Spring Boot控制器(1.5.16.RELEASE)返回org.json JSONObject。 I'm getting an extra map object in it. 我在其中得到了一个额外的地图对象。
My code- 我的代码-
@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 pom.xml
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
expected Json 预期的杰森
{
"message": "login success",
"status": true
}
I'm getting JSON 我正在获取JSON
{
"map": {
"message": "login success",
"status": true
}
}
I dont know why im getting extra map object in my JSONObject. 我不知道为什么我在JSONObject中获得了额外的地图对象。
I think the problem is that the default Springboot JSON serializer is Jackson and it does not know what to do with the JSONObject
so it wraps it into a Map<String, Object>
where the key is map
and the value is your JSONObject
. 我认为问题在于默认的Springboot JSON序列化器是Jackson,它不知道如何处理JSONObject
因此将其包装到Map<String, Object>
中,键为map
,值为您的JSONObject
。 In you case, It would be lot simpler to just use a Map: 对于您而言,仅使用Map会简单得多:
@GetMapping(value = Constants.API_LOGIN)
public ResponseEntity<Object> login(@RequestParam String userName, @RequestParam String password) throws JSONException {
.....
Map<String, Object> json = new HashMap<>();
json.put("status", true);
json.put("message", "login success");
return ResponseEntity.status(HttpStatus.OK).body(json);
}
Or I guess an alternative solution would be to simply return a String stick json.toString
in the body 或者我猜想替代的解决方案是简单地在主体中返回String Stick json.toString
@GetMapping(value = Constants.API_LOGIN)
public String login(@RequestParam String userName, @RequestParam String password) throws JSONException {
....
JSONObject json = new JSONObject();
json.put("status", true);
json.put("message", "login success");
return ResponseEntity.status(HttpStatus.OK).body(json.toString());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.