Java MongoDB:状态应为:w是整数
我正在尝试编写REST web服务以获取MongoClient的对象。Java MongoDB:状态应为:w是整数,java,mongodb,rest,microservices,Java,Mongodb,Rest,Microservices,我正在尝试编写REST web服务以获取MongoClient的对象。我的REST web服务如下所示: @SuppressWarnings({ "rawtypes", "unchecked", "deprecation" }) @RequestMapping(path = "/testMongo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON) @ResponseBody publ
我的REST web服务如下所示:
@SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
@RequestMapping(path = "/testMongo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON)
@ResponseBody
public MongoClient testCredentials(@RequestBody MongoConnect mongoConnect) {
LOGGER.info("MongoController:: testCredentials()::start");
String ipAddress = "";
Integer port = 0;
String username = "";
String password = "";
String result = "";
MongoClient mongo = null;
MongoCredential mongoCredential = null;
String dbName = "admin";
MongoConnectResponse mongoConnectResponse = new MongoConnectResponse();
try {
if (mongoConnect != null) {
ipAddress = mongoConnect.getIpAddress();
port = mongoConnect.getPort();
username = mongoConnect.getUsername();
password = mongoConnect.getPassword();
if ((username == null || username.equalsIgnoreCase(MongoConstants.EMPTY_STRING))
&& (password == null || password.equalsIgnoreCase(MongoConstants.EMPTY_STRING))) {
mongo = new MongoClient(ipAddress, port);
} else {
mongoCredential = MongoCredential.createScramSha1Credential(username, dbName,
password.toCharArray());
mongo = new MongoClient(new ServerAddress(ipAddress, port), Arrays.asList(mongoCredential));
}
DB db = mongo.getDB(dbName);
db.getCollectionNames();
result = MongoConstants.SUCCESS;
} else {
result = MongoConstants.INVALID_REQUEST;
}
LOGGER.info("MongoController:: testCredentials()::end");
} catch (Throwable e) {
if (e.getMessage().contains(MongoConstants.AUTHENTICATION_FAILED)) {
result = MongoConstants.AUTHENTICATION_FAILED;
} else if (e.getMessage().contains(MongoConstants.NOT_AUTHORIZED)) {
result = MongoConstants.USERNAME_PWD_REQD;
} else if (e.getMessage().contains(MongoConstants.TIMED_OUT)) {
result = MongoConstants.NO_SERVER_FOUND + ipAddress + MongoConstants.PORT + port;
} else {
result = e.getMessage() + MongoConstants.CAUSE + e.getLocalizedMessage();
e.printStackTrace();
}
LOGGER.info("MongoController:: testCredentials()::end");
}
return mongo;
//}
}
然而,当我试图通过邮递员对其进行测试时,我得到了以下响应:
{
“时间戳”:1508136268005,
“地位”:500,
“错误”:“内部服务器错误”,
“异常”:“org.springframework.http.converter.HttpMessageNotWritableException”,
“消息”:“无法写入内容:状态应为:w为整数(通过引用链:com.mongodb.MongoClient[\“writeConcern\”]->com.mongodb.writeConcern[\“w\”);嵌套异常为com.fasterxml.jackson.databind.JsonMappingException:状态应为:w为整数(通过引用链:com.mongodb.MongoClient[\“writeConcern\”)->com.mongodb.WriteConcern[\“w\”])“,
“路径”:“/testMongo”
}
请帮忙。我只是猜测一下,但您的用户名或密码中是否有“w:”@dnickless,用户名是“admin”,密码是“password”,听起来像是驱动程序不兼容问题:尝试将所有库更新为匹配的版本!我只是在这里猜测,但您的用户名或密码中是否有“w:”@dnickless,用户名是“admin”,密码是“password”,听起来像是驱动程序不兼容问题:尝试将所有库更新为匹配版本!