小编ale*_*lex的帖子

Spring 3.2和Jackson 2:添加自定义对象映射器

我正在Spring MVC中开发一个REST Web服务.我需要改变杰克逊2序列化mongodb objectids的方式.我不知道该怎么办,因为我找到了jackson 2的部分文档,我做的是创建一个自定义序列化器:

public class ObjectIdSerializer extends JsonSerializer<ObjectId> {


    @Override
    public void serialize(ObjectId value, JsonGenerator jsonGen,
            SerializerProvider provider) throws IOException,
            JsonProcessingException {
        jsonGen.writeString(value.toString());
    }
}
Run Code Online (Sandbox Code Playgroud)

创建一个ObjectMapper

public class CustomObjectMapper extends ObjectMapper {

    public CustomObjectMapper() {
        SimpleModule module = new SimpleModule("ObjectIdmodule");
        module.addSerializer(ObjectId.class, new ObjectIdSerializer());
        this.registerModule(module);
    }

}
Run Code Online (Sandbox Code Playgroud)

然后注册映射器

<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <bean
            class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper">
                <bean class="my.package.CustomObjectMapper"></bean>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>
Run Code Online (Sandbox Code Playgroud)

我的CustomConverter永远不会被调用.我认为CustomObjectMapper定义是错误的,我从jackson 1.x的一些代码改编它

在我的控制器中,我正在使用@ResponseBody.我哪里做错了?谢谢

java spring json spring-mvc jackson

14
推荐指数
1
解决办法
1万
查看次数

Spring数据mongodb查询自动将String转换为ObjectId

标题可能不是很清楚,这里是问题

我正在以这种形式执行更新:

db.poi.update({
  _id: ObjectId("50f40cd052187a491707053b"),
  "votes.userid": {
    "$ne": "50f5460d5218fe9d1e2c7b4f"
  }
},
{
  $push: {
    votes: {
      "userid": "50f5460d5218fe9d1e2c7b4f", 
      "value": 1
    }
  },
  $inc: { "score":1 }
})
Run Code Online (Sandbox Code Playgroud)

仅当没有具有相同用户 ID 的文档时才在数组中插入文档(解决方法,因为唯一索引不适用于数组)。该代码在 mongo 控制台中运行良好。从我的应用程序中,我正在使用这个:

@Override
public void vote(String id, Vote vote) {
    Query query = new Query(Criteria.where("_id").is(id).and("votes.userid").ne(vote.getUserid()));
    Update update = new Update().inc("score", vote.getValue()).push("votes", vote);
    mongoOperations.updateFirst(query, update, Poi.class);
}
Run Code Online (Sandbox Code Playgroud)

如果作为“userid”,我使用一个不能是 mongo ObjectId 的字符串,这很好用,但是如果我在示例中使用该字符串,则执行的查询会像这样转换(来自 mongosniff):

update  flags:0 q:{ _id: ObjectId('50f40cd052187a491707053b'), votes.userid: { $ne: ObjectId('50f5460d5218fe9d1e2c7b4f') } } o:{ $inc: { score: 1 }, $push: { …
Run Code Online (Sandbox Code Playgroud)

java mongodb spring-data

6
推荐指数
1
解决办法
2万
查看次数

匕首活动图和分离的模块

我有一个Android应用程序,我正在尝试使用匕首.我决定使用全局图+活动图.我有三个模块:

AndroidModule

@Module(library = true)
public class AndroidModule {

  private final MApplication mApplication;

  public AndroidModule(MyApplication application) {
    mApplication = application;
  }

  @Provides @Singleton @ForApplication Context provideApplicationContext() {
    return mApplication;
  }
}
Run Code Online (Sandbox Code Playgroud)

MyAppModules

@Module(
    injects = {
            MainActivity.class,
            AddFragment.class,
            AddMapFragment.class,
            MyActivity.class,
            ListFragment.class,
            MyMapFragment.class,
            RetainFragment.class,
            SingleActivity.class,
            UserActivity.class,
            UserLoginFragment.class,
            UserProfileActivity.class,
            UserProfileFragment.class,
            UserRegisterFragment.class,
            WelcomeFragment.class
    },
    complete = false,
    library = true,
    includes = ActivityModule.class
)
public class MyAppModule {

  private final Bus mBus = new Bus();

  @Provides @Singleton Bus provideBus() {
    return mBus;
  } …
Run Code Online (Sandbox Code Playgroud)

android dependency-injection dagger android-volley

5
推荐指数
1
解决办法
2773
查看次数

使用Postgis和Spring休眠空间:无法获取空间数据

我有这个问题:我可以使用地理类型列写入Postgis表,并且数据输入正确。当我尝试取回它时,出现java.lang.IllegalArgumentException:无法转换org.postgresql.util.PGobject类型的对象

我正在使用Hibernate Spatial 4.0M1,Postgis 2.0.2,Postgresql 9.1.7和Spring 3.2,Postgis-jdbc 2.0.2,我也尝试了1.5.2和Postgresql-jdbc 9.1-901。

这是我课程的相关部分

public class Poi {
@Id
@GeneratedValue(generator="t_pois_poisid_seq")
private long poisid;
@Column(name="name")
private String name;
@Column(name="userid")
private int owner;
//@Column(columnDefinition = "Geometry", name="location")
@Type(type="org.hibernate.spatial.GeometryType")
private Point location;


public Poi(){}

public Poi(String name, int owner, String wktPoint){
    this.name = name;
    this.owner = owner;
    WKTReader fromText = new WKTReader();
    Geometry geom = null;
    try{
        geom = fromText.read(wktPoint);
    }catch(ParseException e){
        throw new RuntimeException("Not a WKT string:" + wktPoint);
    }
    if (!geom.getGeometryType().equals("Point")) {
        throw new RuntimeException("Geometry …
Run Code Online (Sandbox Code Playgroud)

postgresql spring hibernate postgis

1
推荐指数
1
解决办法
3165
查看次数