You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As we know we have to add link to resources. I did a mapping from String to Object with hateoas as below:
@GetMapping("/{role}")
public ResponseEntity getRoleInfo(@PathVariable Role role) {
The formatter works very well. But when I try to get link as below:
Link selfLink = linkTo(methodOn(RoleController.class).getRoleInfo(role)).withSelfRel();
error like below:
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [@org.springframework.web.bind.annotation.PathVariable au.com.othera.auth.entity.Role] to type [java.lang.String]
Configuration like below:
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter{
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new RoleStringConvertor());
}
}
formatter like below
import java.text.ParseException;
import java.util.Locale;
@Component
public class RoleFormatter implements Formatter<Role> {
private RoleService roleService;
@Autowired
public RoleFormatter(RoleService roleService) {
this.roleService = roleService;
}
@Override
public Role parse(String roleId, Locale locale) throws ParseException {
return roleService.getRoleInfo(roleId);
}
@Override
public String print(Role role, Locale locale) {
return role.getId();
}
}
Converter like below:
@Component
public class RoleStringConvertor implements Converter<Role, String>{
@Override
public String convert(Role source) {
return source.getId();
}
}
Spring HATEOAS, in order to support the static helpers methods like linkTo and methodOn, has a fixed list of message converters. Hence, it can't pick up dynamically built ones in the application context.
gregturn
changed the title
Formatter and converter can not working to mapping object to String in Hateoas Style
Custom converter doesn't with with @PathVariable in Spring MVC method
Mar 19, 2018
As we know we have to add link to resources. I did a mapping from String to Object with hateoas as below:
The formatter works very well. But when I try to get link as below:
error like below:
Configuration like below:
formatter like below
Converter like below:
Spring version is
Any Suggestion or any configuration wrong?
The text was updated successfully, but these errors were encountered: