Skip to content

Data binding with immutable objects (Kotlin / Lombok / @ConstructorProperties) [SPR-15199] #19763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue Jan 27, 2017 · 5 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Jan 27, 2017

Benjamin M opened SPR-15199 and commented

I'm trying to integrate Lombok with my Spring Project. Now I came across the first issue:

@lombok.Value
public class MyGetParamContainer {
    String foo;
    int bar;
}
@RequestMapping(...)
public void foo(MyGetParamContainer params) {
    // ...
}

This will result in an Exception:

org.springframework.beans.BeanInstantiationException: Failed to instantiate [MyGetParamContainer]: No default constructor found

This happens because it's an immutable object, which sets all its values though its constructor.

Though it would be really nice if Spring could take care of @ConstructorProperties if there's no default constructor.


Affects: 4.3.6, 5.0 M4

Issue Links:

Referenced from: commits fa4d139, 65ba865

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

This connects nicely to an ongoing theme of ours: allowing for a more functional style. Based on the JavaBeans ConstructorProperties mechanism (which we support for constructor argument resolution already) but also against Java 8 parameter name resolution, we'll consider a corresponding mode of data binding for 5.0 RC1.

On a related note, Jackson supports ConstructorProperties for JSON object binding as of 2.7:
FasterXML/jackson-databind#905

@spring-projects-issues
Copy link
Collaborator Author

Benjamin M commented

Yeah, I know that Jackson supports this.

I already can use Lombok with @RequestBody and @ResponseBody, which is awesome :)

@spring-projects-issues
Copy link
Collaborator Author

Aliaksei Zhynhiarouski commented

So, implementing this feature, it will also will add support of Kotlin data classes?

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Good point, this should easily work with Kotlin data classes as well...

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

We're now automatically detecting data classes with a single public constructor, resolving the constructor arguments against the request parameters, as long as the parameter names are retained or an @ConstructorProperties annotation is declared. This works for Spring MVC as well as WebFlux.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants