Best practice to post in spring data rest












1















I would like to know what is the best practice to do the post in this class.



@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"
}


What is the best practice to pass the "id" in the JSON? Directly with the id or with the url?
If I have to proceed with the url, how can I extract the id from it?










share|improve this question




















  • 1





    Generally, I think you will find that most JSON payloads will contain identifiers as regular data elements. If you want to provide 'resource identifiers' as well, the best way to do that is to use HATEOAS links. Spring provides a straightforward way to include HATEOAS links.

    – Tom Drake
    Nov 26 '18 at 21:27











  • As a dependency I used spring-data-rest, so I do not need to generate ResourceSupport, Link, etc. The problem is that I need to extract the id from the link. @Tom Drake

    – Francesco
    Nov 27 '18 at 17:59













  • If you're web-service endpoint is being called, then the identifier should be defined as a Path Parameter. If you're simply trying to parse URLs to obtain id's from json that you generated somewhere else, then I think you're making your life too difficult. I'd add these id's as unadorned data elements to your json payloads, and then just use them in the places that you need to use them. Parsing urls makes your application brittle, in that if those urls change, so will your parsing logic.

    – Tom Drake
    Nov 27 '18 at 21:42











  • Since the url make my application brittle, I will pass the id. But at other points in the program I use the uri. Also in this case I have to pass the id or can I leave the uri directly? { "name": "vitamine", "unit": "kg", "source": "computed value", "category": "nutrionalValueCategories/1" }@TomDrake

    – Francesco
    Nov 29 '18 at 19:15













  • You can and probably should do both. URL links are provided for clients that want to use the url without having to know how to construct urls to your various rest endpoints. But the identifiers stored in fields in your json payloads can be used directly in your services for whatever purposes they are intended for.

    – Tom Drake
    Nov 29 '18 at 19:19
















1















I would like to know what is the best practice to do the post in this class.



@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"
}


What is the best practice to pass the "id" in the JSON? Directly with the id or with the url?
If I have to proceed with the url, how can I extract the id from it?










share|improve this question




















  • 1





    Generally, I think you will find that most JSON payloads will contain identifiers as regular data elements. If you want to provide 'resource identifiers' as well, the best way to do that is to use HATEOAS links. Spring provides a straightforward way to include HATEOAS links.

    – Tom Drake
    Nov 26 '18 at 21:27











  • As a dependency I used spring-data-rest, so I do not need to generate ResourceSupport, Link, etc. The problem is that I need to extract the id from the link. @Tom Drake

    – Francesco
    Nov 27 '18 at 17:59













  • If you're web-service endpoint is being called, then the identifier should be defined as a Path Parameter. If you're simply trying to parse URLs to obtain id's from json that you generated somewhere else, then I think you're making your life too difficult. I'd add these id's as unadorned data elements to your json payloads, and then just use them in the places that you need to use them. Parsing urls makes your application brittle, in that if those urls change, so will your parsing logic.

    – Tom Drake
    Nov 27 '18 at 21:42











  • Since the url make my application brittle, I will pass the id. But at other points in the program I use the uri. Also in this case I have to pass the id or can I leave the uri directly? { "name": "vitamine", "unit": "kg", "source": "computed value", "category": "nutrionalValueCategories/1" }@TomDrake

    – Francesco
    Nov 29 '18 at 19:15













  • You can and probably should do both. URL links are provided for clients that want to use the url without having to know how to construct urls to your various rest endpoints. But the identifiers stored in fields in your json payloads can be used directly in your services for whatever purposes they are intended for.

    – Tom Drake
    Nov 29 '18 at 19:19














1












1








1








I would like to know what is the best practice to do the post in this class.



@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"
}


What is the best practice to pass the "id" in the JSON? Directly with the id or with the url?
If I have to proceed with the url, how can I extract the id from it?










share|improve this question
















I would like to know what is the best practice to do the post in this class.



@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"
}


What is the best practice to pass the "id" in the JSON? Directly with the id or with the url?
If I have to proceed with the url, how can I extract the id from it?







java json spring spring-boot hateoas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 19 '18 at 11:53









RAM

716517




716517










asked Nov 26 '18 at 20:03









FrancescoFrancesco

62




62








  • 1





    Generally, I think you will find that most JSON payloads will contain identifiers as regular data elements. If you want to provide 'resource identifiers' as well, the best way to do that is to use HATEOAS links. Spring provides a straightforward way to include HATEOAS links.

    – Tom Drake
    Nov 26 '18 at 21:27











  • As a dependency I used spring-data-rest, so I do not need to generate ResourceSupport, Link, etc. The problem is that I need to extract the id from the link. @Tom Drake

    – Francesco
    Nov 27 '18 at 17:59













  • If you're web-service endpoint is being called, then the identifier should be defined as a Path Parameter. If you're simply trying to parse URLs to obtain id's from json that you generated somewhere else, then I think you're making your life too difficult. I'd add these id's as unadorned data elements to your json payloads, and then just use them in the places that you need to use them. Parsing urls makes your application brittle, in that if those urls change, so will your parsing logic.

    – Tom Drake
    Nov 27 '18 at 21:42











  • Since the url make my application brittle, I will pass the id. But at other points in the program I use the uri. Also in this case I have to pass the id or can I leave the uri directly? { "name": "vitamine", "unit": "kg", "source": "computed value", "category": "nutrionalValueCategories/1" }@TomDrake

    – Francesco
    Nov 29 '18 at 19:15













  • You can and probably should do both. URL links are provided for clients that want to use the url without having to know how to construct urls to your various rest endpoints. But the identifiers stored in fields in your json payloads can be used directly in your services for whatever purposes they are intended for.

    – Tom Drake
    Nov 29 '18 at 19:19














  • 1





    Generally, I think you will find that most JSON payloads will contain identifiers as regular data elements. If you want to provide 'resource identifiers' as well, the best way to do that is to use HATEOAS links. Spring provides a straightforward way to include HATEOAS links.

    – Tom Drake
    Nov 26 '18 at 21:27











  • As a dependency I used spring-data-rest, so I do not need to generate ResourceSupport, Link, etc. The problem is that I need to extract the id from the link. @Tom Drake

    – Francesco
    Nov 27 '18 at 17:59













  • If you're web-service endpoint is being called, then the identifier should be defined as a Path Parameter. If you're simply trying to parse URLs to obtain id's from json that you generated somewhere else, then I think you're making your life too difficult. I'd add these id's as unadorned data elements to your json payloads, and then just use them in the places that you need to use them. Parsing urls makes your application brittle, in that if those urls change, so will your parsing logic.

    – Tom Drake
    Nov 27 '18 at 21:42











  • Since the url make my application brittle, I will pass the id. But at other points in the program I use the uri. Also in this case I have to pass the id or can I leave the uri directly? { "name": "vitamine", "unit": "kg", "source": "computed value", "category": "nutrionalValueCategories/1" }@TomDrake

    – Francesco
    Nov 29 '18 at 19:15













  • You can and probably should do both. URL links are provided for clients that want to use the url without having to know how to construct urls to your various rest endpoints. But the identifiers stored in fields in your json payloads can be used directly in your services for whatever purposes they are intended for.

    – Tom Drake
    Nov 29 '18 at 19:19








1




1





Generally, I think you will find that most JSON payloads will contain identifiers as regular data elements. If you want to provide 'resource identifiers' as well, the best way to do that is to use HATEOAS links. Spring provides a straightforward way to include HATEOAS links.

– Tom Drake
Nov 26 '18 at 21:27





Generally, I think you will find that most JSON payloads will contain identifiers as regular data elements. If you want to provide 'resource identifiers' as well, the best way to do that is to use HATEOAS links. Spring provides a straightforward way to include HATEOAS links.

– Tom Drake
Nov 26 '18 at 21:27













As a dependency I used spring-data-rest, so I do not need to generate ResourceSupport, Link, etc. The problem is that I need to extract the id from the link. @Tom Drake

– Francesco
Nov 27 '18 at 17:59







As a dependency I used spring-data-rest, so I do not need to generate ResourceSupport, Link, etc. The problem is that I need to extract the id from the link. @Tom Drake

– Francesco
Nov 27 '18 at 17:59















If you're web-service endpoint is being called, then the identifier should be defined as a Path Parameter. If you're simply trying to parse URLs to obtain id's from json that you generated somewhere else, then I think you're making your life too difficult. I'd add these id's as unadorned data elements to your json payloads, and then just use them in the places that you need to use them. Parsing urls makes your application brittle, in that if those urls change, so will your parsing logic.

– Tom Drake
Nov 27 '18 at 21:42





If you're web-service endpoint is being called, then the identifier should be defined as a Path Parameter. If you're simply trying to parse URLs to obtain id's from json that you generated somewhere else, then I think you're making your life too difficult. I'd add these id's as unadorned data elements to your json payloads, and then just use them in the places that you need to use them. Parsing urls makes your application brittle, in that if those urls change, so will your parsing logic.

– Tom Drake
Nov 27 '18 at 21:42













Since the url make my application brittle, I will pass the id. But at other points in the program I use the uri. Also in this case I have to pass the id or can I leave the uri directly? { "name": "vitamine", "unit": "kg", "source": "computed value", "category": "nutrionalValueCategories/1" }@TomDrake

– Francesco
Nov 29 '18 at 19:15







Since the url make my application brittle, I will pass the id. But at other points in the program I use the uri. Also in this case I have to pass the id or can I leave the uri directly? { "name": "vitamine", "unit": "kg", "source": "computed value", "category": "nutrionalValueCategories/1" }@TomDrake

– Francesco
Nov 29 '18 at 19:15















You can and probably should do both. URL links are provided for clients that want to use the url without having to know how to construct urls to your various rest endpoints. But the identifiers stored in fields in your json payloads can be used directly in your services for whatever purposes they are intended for.

– Tom Drake
Nov 29 '18 at 19:19





You can and probably should do both. URL links are provided for clients that want to use the url without having to know how to construct urls to your various rest endpoints. But the identifiers stored in fields in your json payloads can be used directly in your services for whatever purposes they are intended for.

– Tom Drake
Nov 29 '18 at 19:19












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53488212%2fbest-practice-to-post-in-spring-data-rest%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53488212%2fbest-practice-to-post-in-spring-data-rest%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)