我使用java 8和以下spring规范:
<spring.version>4.2.3.RELEASE</spring.version>
<spring.security.version>4.0.3.RELEASE</spring.security.version>
<spring.data.version>1.8.1.RELEASE</spring.data.version>
我有一个显然有_id的文档,它工作得很好,但我有另一个字段存储另一个_id的引用,这是我期望的JSON
{
"_id" : ObjectId("56c8f330d4c65e543ceac8de"),
"companyId" : ObjectId("56c8f330d4c65e543ce8c8de"),
"name" : "COMPANY S.R.L",
more fields......
}
我创建了以下转换器:
@Component
public class PurchaseWriterConverter implements Converter<Purchase, DBObject>
{
@Override public DBObject convert(Purchase source)
{
DBObject dbo = new BasicDBObject();
dbo.put("_id", source.getId());
**ObjectId objCompany = new ObjectId(source.getCompany().toString(16));
dbo.put("company", objCompany);**
dbo.put("supplier", source.getSupplier());
dbo.put("status", source.getStatus().toString());
dbo.put("attendant", source.getAttendant());
dbo.put("cashier", source.getCashier());
List<Object> orders = new BasicDBList();
for (OrderWrapper order : source.getOrders())
{
DBObject orderDBObject = new BasicDBObject();
orderDBObject.put("description", order.getDescription());
orderDBObject.put("basePrice", order.getBasePrice());
orderDBObject.put("grossWeight", order.getGrossWeight());
orderDBObject.put("netWeight", order.getNetWeight());
orderDBObject.put("subtotal", order.getSubtotal());
orderDBObject.put("totalWeight", order.getTotalWeight());
orders.add(orderDBObject);
}
dbo.put("orders", orders);
dbo.put("createDate", source.getCreateDate());
dbo.put("updateDate", source.getUpdateDate());
dbo.put("approvedBy", source.getApprovedBy());
dbo.put("createDate", source.getCreateDate());
dbo.put("updateDate", source.getUpdateDate());
dbo.removeField("_class");
return dbo;
}
}
以下行无效
dbo.put(&#34; _id&#34;,source.getId()); ** ObjectId objCompany = new ObjectId(source.getCompany()。toString(16));
所以,我尝试创建另一个转换器,以便将BigInteger转换为ObjectId,就像这样
公共类BigIntegerToObjectIdConverter实现Converter {
@Override
public ObjectId convert(BigInteger source) {
return source == null ? null : new ObjectId(source.toString());
}
}
但是我得到了同样的错误:
org.springframework.core.convert.ConversionFailedException: Failed to convert from type com.xxxxxxxxxxx.entity.Purchase to type com.mongodb.DBObject for value 'Purchase{company=1, orders=[com.xxxxxxx.equilibrium.wrapper.OrderWrapper@9cd3f65], supplier=26857458392532697059830357595, approvedBy='', status=TO_BE_APPROVED, attendant='User Admin', cashier=''}'; nested exception is java.lang.IllegalArgumentException: invalid ObjectId [1]
我作为biginteger发送的值是&#34; 1&#34;。
任何人都可以给我一些关于如何将BigInteger转换为ObjectId或建议不同内容的指示。
答案 0 :(得分:1)
我刚才在这里回答了类似的问题https://stackoverflow.com/a/46508892/4660481
有效的demand <- ts(
c(39.8, 33.5, 40.6, 23.6, 11.9, 12.3, 6.2, 17.1, 10.8, 18, 1, -10.7,
-10.4, -16.5, -5.6, -11.9, -4.7, -21.7, -33.4, -33.1, -39.2, -28.2,
-34.6, -27.4, -44.4, -56.1, -55.7, -61.8, -50.9, -57.2, -50.1),
frequency = 7
)
plot(demand)
可以由正好24个十六进制字符构成。在这种情况下,您的ObjectId
将返回.toString(16)
,其长度不是24。在这种情况下,您需要的是"1"
。您可以通过使用0填充来解决此问题:
"000000000000000000000001"