小编egh*_*ghe的帖子

如何在 Jackson 的 ObjectMapper 上添加 Jdk8Module

我正在使用杰克逊 2.9.6。我有一个控制器,我试图在其中发送一个可选作为我的字段之一。当我收到控制器的响应时,我总是以这种格式得到它{"field":{"present":true}}(如这个问题所示)。

基本上我有一个 RestTemplate bean 配置如下:

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
        </list>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

我想添加 Jdk8Module 作为序列化过程的一部分。我注意到有一个MappingJackson2HttpMessageConverter(ObjectMapper mapper)接受 ObjectMapper 的构造函数,我正在考虑创建一个 ObjectMapper bean,它将向其注册 Jdk8Module (使用registerModule(Module module)在 ObjectMapper 类中找到的公共方法),以便我可以像这样传递该模块:

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <constructor-arg name="objectMapper" ref="ObjectMapperWithJDK8Bean"></constructor-arg>
            </bean>
        </list>
    </property>
</bean>

<bean id="ObjectMapperWithJDK8Bean" class="com.fasterxml.jackson.databind.ObjectMapper">
* Pass in com.fasterxml.jackson.datatype.jdk8.Jdk8Module here via the method *
</bean>
Run Code Online (Sandbox Code Playgroud)

但我当前面临的问题是registModule创建bean时如何从xml文件中调用方法?我正在使用 Spring 4.1。我是 Spring 新手,所以这非常具有挑战性!

java spring json jackson

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

如何使用 biginteger 对两个边界之间的数字求和

我试图总结 2 个给定数字之间的所有数字,不包括边界。例如,addNumbers("5", "8")由于 6+7=13,应该返回 13。这是我目前拥有的功能。

 public static BigInteger addNumbers(String from, String to) {
  BigInteger total = new BigInteger("0");
  BigInteger startingBoundary = new BigInteger(from);
  BigInteger finishingBoundary = new BigInteger(to);

  if (startingBoundary.compareTo(finishingBoundary) < 0) {
     startingBoundary = new BigInteger(from);
     finishingBoundary = new BigInteger(to);
  } else {
     finishingBoundary = new BigInteger(from);
     startingBoundary = new BigInteger(to);
  }

  while (startingBoundary.compareTo(finishingBoundary) != 0 )  {
     System.out.println("Starting boundary:" + startingBoundary.intValue());
     System.out.println("Finishing boundary: " + finishingBoundary.intValue());

     total.add(startingBoundary);
     System.out.println("total: "+total.intValue());

     startingBoundary.add(new BigInteger("1"));
  }
  return total;
Run Code Online (Sandbox Code Playgroud)

} …

java biginteger

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

标签 统计

java ×2

biginteger ×1

jackson ×1

json ×1

spring ×1