Skip to content

Instantly share code, notes, and snippets.

@jianhe-fun
Created August 28, 2021 05:55
Show Gist options
  • Save jianhe-fun/444d360bd2244195588d3fcaf54145c8 to your computer and use it in GitHub Desktop.
Save jianhe-fun/444d360bd2244195588d3fcaf54145c8 to your computer and use it in GitHub Desktop.
package jian.he.formatters;
import jian.he.model.PetType;
import jian.he.services.PetTypeService;
import org.springframework.format.Formatter;
import org.springframework.stereotype.Component;
import java.text.ParseException;
import java.util.Collection;
import java.util.Locale;
@Component
public class PetTypeFormatter implements Formatter<PetType> {
private final PetTypeService petTypeService;
public PetTypeFormatter(PetTypeService petTypeService) {
this.petTypeService = petTypeService;
}
@Override
public String print(PetType petType, Locale locale) {
return petType.getName();
}
@Override
public PetType parse(String s, Locale locale) throws ParseException {
Collection<PetType> findPetTypes = petTypeService.findAll();
// System.out.println("findPetTypes Collection test: "+ findPetTypes.size());
for(PetType type: findPetTypes){
if(type.getName().equals(s)){
System.out.println(type.getClass());
System.out.println(type);
return type;
}
}
throw new ParseException("type not found: " + s, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment