我想知道在这堂课中写这篇文章的最佳实践。
@Component
public class IngredientToNutrionalValueIdConverter implements BackendIdConverter {
@Override
public Serializable fromRequestId(String s, Class<?> aClass) {
if (s != null) {
NutrionalValueIngredientId ptid = new NutrionalValueIngredientId();
String[] idParts = s.split("-");
ptid.setIngredientid(Long.valueOf(idParts[0]));
ptid.setNutrionalvalueid(Long.valueOf(idParts[1]));
return ptid;
}
return BackendIdConverter.DefaultIdConverter.INSTANCE.fromRequestId(s, aClass);
}
@Override
public String toRequestId(Serializable s, Class<?> aClass) {
NutrionalValueIngredientId ptid = (NutrionalValueIngredientId) s;
return String.format("%s-%s", ptid.getIngredientid(), ptid.getNutrionalvalueid());
}
@Override
public boolean supports(Class<?> aClass) {
return JoinedNutrionalValueIngredient.class.equals(aClass);
}
}
JSON
{
"id":{
"ingredientid": "ingredients/1",
"nutrionalvalueid": "nutrionalValues/1"
},
"matrixUnit": "50 g a porzione",
"value": 4,
"valueType": "ponderata"
}
在JSON中传递“ id”的最佳实践是什么?直接使用id或url? 如果必须继续处理URL,如何从中提取ID?