我正在尝试编写一个REST Web服务来获取MongoClient的对象。
我的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;
//}
}
然而,当我试图通过Postman进行测试时,我得到以下回复:
{ "时间戳":1508136268005, "状态":500, "错误":"内部服务器错误", " exception":" org.springframework.http.converter.HttpMessageNotWritableException", " message":"无法写内容:状态应该是:w是一个整数(通过参考链:com.mongodb.MongoClient [\" writeConcern \"] - > com.mongodb.WriteConcern [\" W \"]);嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:state应该是:w是一个Integer(通过引用链:com.mongodb.MongoClient [\" writeConcern \"] - > com.mongodb .WriteConcern [\" W \"])&#34 ;, "路径":" / testMongo" }
请帮助。