Spring Webflow DataBinding to immutable objects vi

2019-02-07 03:57发布

Is there any way of using an immutable object as a model within a view-state in Spring webflow? I know Spring webflow generally tends towards setters for this kind of thing, but I was wondering if anyone knew of a custom DataBinder or WebDataBinder that could handle binding the data using a constructor?

I'm also aware there's this (SPR-1488) JIRA task against the problem, which advocated direct field access. Do people advocate this way of doing things? To me it doesn't quite feel right.

Thanks,

Stuart

1条回答
Summer. ? 凉城
2楼-- · 2019-02-07 04:47

I have put an example of how you can do this using Jackson's ObjectMapper (which besides JSON does a good job mapping anything to immutable objects ).

https://gist.github.com/4458079

A couple of things to note is that you can't use @ModelAttribute if you want to use Spring's validation (BindingResult) unless you replace all the argument resolvers. However @Valid (with validation) should work and @RequestBody will also work (with out validation) with my solution.

Although its using Jackson to map request parameters to objects there is no JSON processing involved. If you want that see MappingJacksonHttpMessageConverter.

As a feeble shameless plug I needed this in conjunction with my Immutable ORM called: JIRM as I prefer immutable objects for message driven architectures.

查看更多
登录 后发表回答