小编Spa*_*ski的帖子

Spring RestTemplate与Jackson作为HttpMessageConverter和joda DateTime属性无法反序列化

方案如下.我有一个ObjectMapper(Jackson 2),它注册了一个JodaModule,能够序列化和反序列化Joda DateTime类型.此ObjectMapper使用自定义JSON字符串进行测试,并按预期工作.

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JodaModule());
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+1:00"));
objectMapper.setDateFormat(new ISO8601DateFormat());
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return objectMapper;
Run Code Online (Sandbox Code Playgroud)

我有一个RestTemplateFactory负责实例化RestTemplate,它将以前配置的ObjectMapper bean设置为RestTemplate.

@Configuration
public class RestTemplateFactory {

  @Autowired
  private ObjectMapper objectMapper;

  @Bean
  public RestTemplate createRestTemplate() {
    RestTemplate restTemplate = new RestTemplate();
    List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
    MappingJackson2HttpMessageConverter jsonMessageConverter = new MappingJackson2HttpMessageConverter();
    jsonMessageConverter.setObjectMapper(objectMapper);
    messageConverters.add(jsonMessageConverter);
    // restTemplate.setMessageConverters(messageConverters); // This line was missing, but needs to be here. See answer.
    return restTemplate;
  }
}
Run Code Online (Sandbox Code Playgroud)

现在当我联系webservice时,它无法使用以下错误消息反序列化DateTime对象:

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not instantiate value …
Run Code Online (Sandbox Code Playgroud)

spring jodatime jackson jackson-modules

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

用于jaxb-maven-plugin的IntelliJ bindingDirectory

我想在一个单独的schema.xjb绑定文件中为schema.xsd生成的类型添加globalBinding.我正在使用IntelliJ并且不确定这个问题是maven还是Intellij正在做(因为这个例子在eclipse中按预期运行).我得到的错误是:

org.xml.sax.SAXParseException; systemId: file:/D:/Projects/Location/To/Project/src/main/resources/xsd/schema.xsd; lineNumber: 7; columnNumber: 10; vendor extension bindings (jaxb:extensionBindingPrefixes) are not allowed in the strict mode. Use -extension.
Run Code Online (Sandbox Code Playgroud)

这是我的pom.xml中的build元素:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>1.5</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <!-- The name of your generated source package -->
                <packageName>com.my.model.example</packageName>
                    <schemaDirectory>${project.basedir}/src/main/resources/xsd</schemaDirectory>
                <!-- Well Intellij acts badly when it comes down to binding files, so there is that. -->
                <bindingDirectory>${project.basedir}/src/main/resources/xjb</bindingDirectory>
            </configuration>
        </plugin>

    </plugins>

</build>
Run Code Online (Sandbox Code Playgroud)

这是我的架构位于/ src/main/resources/xsd:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           id="SampleSchema"
           targetNamespace="http://sample.com/namespace"
           elementFormDefault="qualified"
           xmlns="http://sample.com/namespace"
        >

    <xs:element …
Run Code Online (Sandbox Code Playgroud)

jaxb intellij-idea maven jaxb2-maven-plugin

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

使用Gradle for Android进行多项目设置

我在构建Android应用程序时遇到问题.

我有一个主应用程序模块,另一个是google-play-services_lib所需的模块.

我的文件夹结构如下:

ParkingApp
   | 
   |-----> google-play-services_lib (Library Project) 
   |-----> ParkingApp
   |-----> settings.gradle
Run Code Online (Sandbox Code Playgroud)

我的settings.gradle文件如下:

include ':ParkingApp', ':google-play-services_lib'
Run Code Online (Sandbox Code Playgroud)

我的ParkingApp有以下build.gradle.

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile files('libs/android-support-v4.jar')
    compile project(':google-play-services_lib')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 17
    }
}
Run Code Online (Sandbox Code Playgroud)

并且google-play-services_lib具有以下build.gradle:

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android-library'

dependencies { …
Run Code Online (Sandbox Code Playgroud)

android gradle google-play-services android-studio

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

比较两个joda DateTime实例

我在比较两个DateTime对象时遇到问题.

System.out.println(response.getCreationDate().toString()); // will return "2013-12-31T22:59:21.000+01:00", but...

assertThat(response.getCreationDate(), equalTo(new DateTime("2013-12-31T22:59:21+01:00"))); // will throw an assertation error with the following error

Expected: <2013-12-31T22:59:21.000+01:00>
 but: was <2013-12-31T22:59:21.000+01:00>
Run Code Online (Sandbox Code Playgroud)

任何人都知道我在这里缺少什么?

顺便说一下,如果你想知道为什么DateTime会显示在GMT + 1:00区域,那么这就是我希望默认情况下我的DateTime对象的时区.

谢谢!

java datetime jodatime

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