Spring boot 2.0 custom converter being ignored

Multi tool use
Spring boot 2.0 custom converter being ignored
I've either completely miss-understood converters, or all the examples/blogs/docs I've found are assuming I've done a step... My understanding is that when I post a String
, the converter will pick it up and give the Controller
a POJO instead.
String
Controller
The problem I have is the converter is never being called.
The payload coming into the controller is a standard JSON API string
{
"data":{
"attributes":{
"created-date":null,
},
"type":"steaks"
}
}
The converter:
@Slf4j
@Configuration
public class SteakConverter implements Converter<String, Steak> {
@Override
public Steak convert(String value) {
log.info("converter value {}", value);
return new Steak.builder().build();
}
}
The controller:
@Slf4j
@CrossOrigin
@RestController
public class SteakController {
@PostMapping("/steaks")
public ResponseEntity<String> create(@RequestBody Steak steak) {
}
}
From reading all the blogs and docs I can find this should be all that's needed. However I've also tried manually registering with the following:
@Slf4j
@EnableWebMvc
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addFormatters(FormatterRegistry registry) {
log.info("Converter registered");
registry.addConverter(new SteakConverter());
}
}
This is called during application startup.
The only thing I can think of is that the converter/controller doesn't think the payload is in fact a String, so is ignoring it?
How can I debug this, or just make it work?
Here is a working sample application that shows my problem.
Cheers
@Component
@Configuration
Done and done, still being skipped.
– Chris Turner
Jul 2 at 0:12
Remove
@EnableWebMvc
from WebConfig, as this annotation disables Spring Boot Auto Configuration.– fg78nc
Jul 2 at 0:15
@EnableWebMvc
Done and still no change...
– Chris Turner
Jul 2 at 0:16
Converter will be used only to convert form field, not payload.
– fg78nc
Jul 2 at 0:17
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You should make your converter a bean. Add
@Component
to it's type declaration, instead of@Configuration
and inject it into FormatterRegistry.– fg78nc
Jul 2 at 0:11