Skip to content

make Custom jackson module fails #513

@Djacke

Description

@Djacke

My system:

  • springbootversion 2.0.1.RELEASE
  • springCloudVersion Finchley.M9
  • java 1.8
  • org.springframework.boot:spring-boot-starter-webflux
  • org.javamoney:moneta:1.2.1
  • org.springframework.restdocs:spring-restdocs-webtestclient

custom SimpleModule

 @Bean
  public SimpleModule moneyModule() {
    return new MoneyModule();
  }

public MoneyModule() {
      addSerializer(Money.class, new MoneySerializer());
      addValueInstantiator(Money.class, new MoneyInstantiator());
  }

Integration test

@RunWith(SpringRunner.class)
@ActiveProfiles("integTest")
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class XxxxHandlerTest{
    @Autowired
  WebTestClient webTestClient;

  @Rule
  public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();

  @Autowired
  ApplicationContext context;

@Before
  public void init() throws Exception {
    this.webTestClient = WebTestClient.bindToApplicationContext(this.context)
        .configureClient()
        .baseUrl("http://local.com.cn")
        .filter(WebTestClientRestDocumentation
            .documentationConfiguration(this.restDocumentation)
            .operationPreprocessors()
            .withResponseDefaults(prettyPrint())
        )
        .build();
  }

 @Test
  public void testStoreVoucherEntry() {
    Operator mockUser = Operator.builder().name("张会计").id(ObjectId.get().toString()).build();
    List<AccountingEntry> accountingEntries = Arrays.asList(AccountingEntry.builder()
        .code("12121").summary("销售票据").debit(Money.of(100, "CNY"))
        .credit(Money.of(100, "CNY")).build());
    VoucherPost voucherPost = VoucherPost.builder().operator(mockUser)
        .accountingEntries(accountingEntries).build();
    webTestClient.post()
        .uri(mockUrl)
        .body(BodyInserters.fromObject(voucherPost))
        .exchange().expectStatus().isOk();
}

The above code test error :

org.springframework.core.codec.DecodingException: JSON decoding error: Cannot construct instance of `org.javamoney.moneta.Money` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `org.javamoney.moneta.Money` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)

When I remove the following code,test code is working properly

 this.webTestClient = WebTestClient.bindToApplicationContext(this.context)
        .configureClient()
        .baseUrl("http://local.com.cn")
        .filter(WebTestClientRestDocumentation
            .documentationConfiguration(this.restDocumentation)
            .operationPreprocessors()
            .withResponseDefaults(prettyPrint())
        )
        .build();

I guess the webtestclient's configuration has caused it to ignore the custom jackson module, so I would like to know how to solve this problem. Maybe there is no problem at all, but my configuration is wrong. Please give me some advice. Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions