Skip to content

Custom converter doesn't with with @PathVariable in Spring MVC method #702

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
caoyuanqi opened this issue Mar 5, 2018 · 3 comments
Closed

Comments

@caoyuanqi
Copy link

caoyuanqi commented Mar 5, 2018

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 version is

springBootVersion = '1.5.3.RELEASE'
springCloudVersion = 'Dalston.SR1'

Any Suggestion or any configuration wrong?

@gregturn
Copy link
Contributor

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.

Duplicates #654

@gregturn 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
@izeye
Copy link
Contributor

izeye commented May 2, 2018

This looks meant to be closed as a duplicate based on the above @gregturn 's comment.

@gregturn
Copy link
Contributor

Indeed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants