我遇到的问题与链接中提到的相同: Mongo spring-data issue with java.util.Currency 我尝试了接受的答案。但它没有工作,我在线上得到NullPointerException:
angular.module('admin', [])
.provider('users', function () {
this.users = 'default';
this.$get = function () {
var that = this;
return {
getUsers: function () {
return that.users;
}
}
};
})
.run(function (users, $http) {
users.users = $http('url'); // and others
})
.controller('test', function ($scope, users) {
$scope.users = users.getUsers();
});
所以我在新类中定义了转换器,如下所示:
new CustomConversions(Arrays.asList(currencyToString, stringToCurrency));
和
@Component
public class CurrencyToStringConverter implements Converter<Currency,String>{
/* (non-Javadoc)
* @see com.fasterxml.jackson.databind.util.Converter#convert(java.lang.Object)
*/
@Override
public String convert(Currency arg0) {
System.out.println("***INSIDE CurrencyToStringConverter***"+arg0);
return arg0.getCurrencyCode();
}
}
我在“customConversions()”中使用它们,如下所示:
@Component
public class StringToCurrencyConverter implements Converter<String,Currency>{
/* (non-Javadoc)
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
*/
@Override
public Currency convert(String arg0) {
System.out.println("***INSIDE StringToCurrencyConverter***"+arg0);
return Currency.getInstance(arg0);
}
}
当spring-boot应用程序启动时,我可以看到以下打印的语句:
@Configuration
public class CustomMongoConfiguration extends AbstractMongoConfiguration {
@Autowired
private Environment env;
@Bean
@Override
public CustomConversions customConversions() {
System.out.println("***CUSTOM MONGO CONVERSIONS***");
List<Converter<?,?>> converters=new ArrayList<>();
converters.add(new CurrencyToStringConverter());
converters.add(new StringToCurrencyConverter());
return new CustomConversions(converters);
}
/* (non-Javadoc)
* @see org.springframework.data.mongodb.config.AbstractMongoConfiguration#getDatabaseName()
*/
@Override
protected String getDatabaseName() {
// TODO Auto-generated method stub
String prop = env.getProperty("spring.data.mongodb.database");
System.out.println("database naem:: "+prop);
return prop;
}
/* (non-Javadoc)
* @see org.springframework.data.mongodb.config.AbstractMongoConfiguration#mongo()
*/
@Override
@Bean
public Mongo mongo() throws Exception {
String prop = env.getProperty("spring.data.mongodb.host");
System.out.println("host naem:: "+prop);
return new MongoClient(prop);
}
}
但是当我尝试从MongoDB获取对象时,它从不在Converter类中调用“转换”方法,而且,我总是得到这个例外:
System.out.println("***CUSTOM MONGO CONVERSIONS***");
System.out.println("database naem:: "+prop);
System.out.println("host naem:: "+prop);
我也跟着这个链接:
Spring not using mongo custom converters
我按照链接中的建议将@Configuration注释更改为类org.springframework.data.mapping.model.MappingException: No property null found on entity class java.util.Currency to bind constructor parameter to!
at org.springframework.data.mapping.model.PersistentEntityParameterValueProvider.getParameterValue(PersistentEntityParameterValueProvider.java:74)
at org.springframework.data.mapping.model.SpELExpressionParameterValueProvider.getParameterValue(SpELExpressionParameterValueProvider.java:63)
at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:71)
at org.springframework.data.convert.ClassGeneratingEntityInstantiator.createInstance(ClassGeneratingEntityInstantiator.java:83)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:251)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:231)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readValue(MappingMongoConverter.java:1186)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.access$200(MappingMongoConverter.java:78)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1134)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getValueInternal(MappingMongoConverter.java:870)
中的@Component。但它也没有用。如果我使用@Component注释,下面的语句将被多次打印。我不知道原因。
CustomMongoConfiguration
我正在使用spring-boot版本1.3.2.RELEASE。
System.out.println("***CUSTOM MONGO CONVERSIONS***");
System.out.println("database naem:: "+prop);
System.out.println("host naem:: "+prop);
顺便说一下,我正在使用<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
来配置mongodb属性,而不是任何基于java的配置:
application.properties
过去三天我一直在努力解决这个问题。任何人都可以帮助我。
答案 0 :(得分:2)
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>SVG</title>
<style type="text/css" media="screen">
body {
background: #eee;
margin: 0;
}
svg {
display: block;
border: 1px solid #ccc;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: #fff;
}
.face {
stroke: #000;
stroke-width: 20px;
stroke-linecap: round
}
</style>
</head>
<body>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-350 -250 700 500">
<circle r="200" class="face" fill="red"/>
<path fill="none" class="face" transform="translate(-396,-230)" d="M487.41,282.411c-15.07,36.137-50.735,61.537-92.333,61.537 c-41.421,0-76.961-25.185-92.142-61.076"/>
<circle id="leftEye" cx="-60" cy="-50" r="20" fill="#00F"/>
<circle id="rightEye" cx="60" cy="-50" r="20" fill="#00F"/>
</svg>
<script type="text/javascript">
document.getElementById('leftEye').addEventListener('mouseover', function (e) {
alert('Left Eye');
});
document.getElementById('rightEye').addEventListener('mouseover', function (e) {
alert('Right Eye');
});
</script>
</body>
</html>
转化已通过b7131b添加,自Currency
开始提供。
请将1.9.0.M1
和@ReadingConverter
添加到@WritingConverter
实施中,以便Converter
而不仅仅是基础CustomConversions
了解这些内容。
答案 1 :(得分:2)
我的坏。问题在于早先在mongodb中插入的数据。早先用不正确的“货币”格式插入mongodb,即,而不仅仅是美元,英镑等字符串值,它具有以下结构:
"currency" : {
"currencyCode" : "GBP",
"defaultFractionDigits" : 2,
"numericCode" : 826
}
代替简单的代码,如:
"currency" : "GBP"
在我编写转换器后,mongo无法解析此结构并抛出此异常:
org.springframework.data.mapping.model.MappingException: No property null found on entity class java.util.Currency to bind constructor parameter to!
at org.springframework.data.mapping.model.PersistentEntityParameterValueProvider.getParameterValue(PersistentEntityParameterValueProvider.java:74)
我认为这种例外具有误导性。无论如何,我想通了。现在两个转换器都被调用了。
最终解决方案对我有用:
@Configuration
public class CustomMongoConfiguration extends AbstractMongoConfiguration {
@Autowired
private MongoProperties mongoProps;
@Bean
@Override
public CustomConversions customConversions() {
System.out.println("***CUSTOM MONGO CONVERSIONS***");
List<Converter<?, ?>> converters = new ArrayList<>();
converters.add(new CurrencyToStringConverter());
converters.add(new StringToCurrencyConverter());
return new CustomConversions(converters);
}
@Override
protected String getDatabaseName() {
return mongoProps.getDatabase();
}
@Override
@Bean
public Mongo mongo() throws Exception {
return new MongoClient(mongoProps.getHost(), mongoProps.getPort());
}
}
根据春季文档http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.converter-disambiguation,您需要将@ReadingConverter
和@WritingConverter
放在转换器上,只有源和版本都是如此。 target是本机类型(例如:String to Long或Long to String)。否则,您不需要在转换器上使用它们。在我的情况下,不需要作为货币的那些不是本机mongo类型。