SpringDataWebAutoConfiguration resolvers conflict
Adding Spring Data JPA to a Spring MVC project (@SpringBootApplication VERSION 1.2.6.RELEASE), I experienced that my POST request parameter wouldn't load anymore. It turned out that a org.springframework.data.repository.support.DomainClassConverter was conflicting with the usual org.springframework.core.convert.support.GenericConversionService#NoOpConverter ; the well converted entity was finally resolved as a String, ending in request parameter's exclusion.
Among the resolvers, DomainClassConverter is introduced by SpringDataWebConfiguration, through @SpringDataWebAutoConfiguration (with pageable and sorteable capabilities). The only way I found to workaround is to explode Spring Boot Application declaration :
//DEBUG DomainClassConverter conflict ; avoid @SpringBootApplication
@Configuration
@ComponentScan
//DEBUG see SpringDataWebConfiguration : pageable & sortable are gone as well !
@EnableAutoConfiguration(exclude = SpringDataWebAutoConfiguration.class)
public class Main extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
I might code another SpringDataWebConfiguration in order to retrieve sorteable and pageable capabilities, but... well... that's not the point !
Did I miss something or is this actually a bug ??
Did I miss something or is this actually a bug ??
Hard to say without seeing some code. Can you share a sample that reproduces the problem please?
I have a complete project as a ZIP ; but cannot drop it in what's the process in these cases plz ?
People often share projects via Dropbox, or similar. If it's in Git, another option is to open a PR against https://github.com/spring-projects/spring-boot-issues or push it to your own open source repo.
there you are ;-) https://www.dropbox.com/s/hmoix9nru3o2e1q/Spring%20MVC%20%26%20Data%20conflict.zip?dl=0
a breakpoint can be put at line 253 (conditional: sourceType.getType().equals(fr.ippon.training.spring.domain.TodoList.class) in GenericConversionService#GenericConverter then replace Main's conf for @SpringBootApplication in order to monitor the pb
Thank you
@tooms4444 I'm struggling to reproduce the problem using the sample you provided. I can't see why the POST in the test would use a converter for TodoList as the method takes a String and a Todo:
@RequestMapping(value = "/lists/{listId}/todos", method = RequestMethod.POST)
public void add(@PathVariable("listId") String listId, @RequestBody Todo todo) {
todoService.newTodo(todo, listId);
}
What have I missed?
I can trigger a failure if I exclude SpringDataWebAutoConfiguration and change the method to take a TodoList:
@RequestMapping("/lists/{listId}/todos.json")
public Collection<Todo> list(@PathVariable("listId") TodoList list) {
return todoService.getTodosByListId(list.getListId());
}
I believe that's the opposite to the problem that you've described. It's also the behaviour that I would expect. Without Spring Data REST's web support the automatic conversion of listId to a TodoList is lost.
Hi! Sorry for the delay. The issue wasn't on TodoController, but on TodoListController. The provided test should'nt be loading... but you've got the wrong setup method (sorry for that) :
@Before
public void setUp() throws Exception {
this.mockMvc = webAppContextSetup(this.context).build();
// service.newTodoList(new TodoList("foo"));
this.mockMvc.perform(post("/lists") //FIXME
.param("description", "foobar"))
.andDo(print());
}
Please consider this test
The problem's triggered by TodoList using a String as its id and also having a single-arg constructor that takes a String. The ID being a String leads to DomainClassConverter asking the conversion service if it can convert a TodoList to a String. This leads to FallbackObjectToStringConverter finding the single-arg constructor that takes a String and it then being used for conversion. If you remove the constructor from TodoList, which I don't think you need anyway, your test will pass.
I'm going to close this issue as what's described above is standard Spring Data Web behaviour, i.e. there's nothing that Spring Boot's doing that's triggering this problem vs if you'd manually enabled it using @EnableSpringDataWebSupport.
You might want to raise a Spring Data issue as I was a little surprised at DomainClassConverter claiming that it matches for the requested conversion purely because of FallbackObjectToStringConverter. /cc @olivergierke
That's unfortunately the way Spring's ConversionService works. There's simply no way to ask "can you convert this without considering the FallbackObjectToStringConverter". Even without Spring Data web support enabled, Spring MVC would discover the constructor taking a String and use it to create an instance of the domain type, wouldn't it?
Even without Spring Data web support enabled, Spring MVC would discover the constructor taking a String and use it to create an instance of the domain type, wouldn't it?
@olivergierke No, I don't think so. The conversion that's being attempted is a TodoList to a @Valid TodoList. This becomes a TodoList to String conversion thanks to the String id on TodoList and this logic in DomainClassConverter.matches:
conversionService.canConvert(sourceType.getType(),
repositories.getRepositoryInformationFor(targetType.getType()).getIdType());
If you take DomainClassConverter out of the picture FallbackObjectToStringConverter doesn't get involved as String is never the target type.
@olivergierke Thanks for the details, I'm not sure at all I understood every ins and outs in Spring Data's involving for that matters through MVC... but what I figured out is that the "String constructor" is interpreted as a deserializer. I have to say that seems odd to me. The implicit convention I got the habit would rather be a static valueOf function. If I'm right with the approach, it doesn't sound quite "POJO friendly" and I guess that I won't be the only one to bother, trying to explain the bug in the future... Is there an article or manual I can address in order to have a better idea of what SpringDataWebAutoConfiguration is about ?