RESTful implementation of factory method design pattern











up vote
1
down vote

favorite












How would you design a RESTful API for a class that uses the factory method pattern to create objects?



Let's say you have class Passport:



public class Passport {
final String firstName;
final String lastName;
final Date birthDate;
State state;

private Passport(State state, String firstName, String lastName, Date birthDate) {
this.state = state;
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
}


public static Passport createPreliminary(String firstName, String lastName, Date birthDate) {
return new Passport(PRELIMINARY, firstName, lastName, birthDate);
}

public static Passport createRegular(String firstName, String lastName, Date birthDate) {
return new Passport(REGULAR, firstName, lastName, birthDate);
}

public void invalidate() {
state = INVALID;
}
}

public enum State {
PRELIMINARY,
REGULAR,
EXPIRED,
INVALID
}


A Passport instance can be created in two different states. After creation only the state property can be changed in a restricted way via state transition methods like invalidate().



What would be the RESTful way to create Passport resources? A POST to /passports including the state property and check on the server-side that the state is either REGULAR or PRELIMINARY and return a BAD REQUEST response if it is an illegal state? Or two different URLs for creating passport resources, one for "regular" and one for "preliminary" passports?










share|improve this question






















  • How would you achive your needs on the Web? You'd ask the server for a resource that teaches you how a request has to be build. The server would respond with a form that offers a client a skeletton what fields and what types are necessary and also provides a convenient submit button to actually perform the request. Whether you create a REGULAR or a PRELIMINARY passport will be determined by probably a dropdown choice. The same can be applied for any REST application as well. Either reuse HTML forms or specify your own general-purpose media-types in that particular case
    – Roman Vottner
    Nov 19 at 18:17















up vote
1
down vote

favorite












How would you design a RESTful API for a class that uses the factory method pattern to create objects?



Let's say you have class Passport:



public class Passport {
final String firstName;
final String lastName;
final Date birthDate;
State state;

private Passport(State state, String firstName, String lastName, Date birthDate) {
this.state = state;
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
}


public static Passport createPreliminary(String firstName, String lastName, Date birthDate) {
return new Passport(PRELIMINARY, firstName, lastName, birthDate);
}

public static Passport createRegular(String firstName, String lastName, Date birthDate) {
return new Passport(REGULAR, firstName, lastName, birthDate);
}

public void invalidate() {
state = INVALID;
}
}

public enum State {
PRELIMINARY,
REGULAR,
EXPIRED,
INVALID
}


A Passport instance can be created in two different states. After creation only the state property can be changed in a restricted way via state transition methods like invalidate().



What would be the RESTful way to create Passport resources? A POST to /passports including the state property and check on the server-side that the state is either REGULAR or PRELIMINARY and return a BAD REQUEST response if it is an illegal state? Or two different URLs for creating passport resources, one for "regular" and one for "preliminary" passports?










share|improve this question






















  • How would you achive your needs on the Web? You'd ask the server for a resource that teaches you how a request has to be build. The server would respond with a form that offers a client a skeletton what fields and what types are necessary and also provides a convenient submit button to actually perform the request. Whether you create a REGULAR or a PRELIMINARY passport will be determined by probably a dropdown choice. The same can be applied for any REST application as well. Either reuse HTML forms or specify your own general-purpose media-types in that particular case
    – Roman Vottner
    Nov 19 at 18:17













up vote
1
down vote

favorite









up vote
1
down vote

favorite











How would you design a RESTful API for a class that uses the factory method pattern to create objects?



Let's say you have class Passport:



public class Passport {
final String firstName;
final String lastName;
final Date birthDate;
State state;

private Passport(State state, String firstName, String lastName, Date birthDate) {
this.state = state;
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
}


public static Passport createPreliminary(String firstName, String lastName, Date birthDate) {
return new Passport(PRELIMINARY, firstName, lastName, birthDate);
}

public static Passport createRegular(String firstName, String lastName, Date birthDate) {
return new Passport(REGULAR, firstName, lastName, birthDate);
}

public void invalidate() {
state = INVALID;
}
}

public enum State {
PRELIMINARY,
REGULAR,
EXPIRED,
INVALID
}


A Passport instance can be created in two different states. After creation only the state property can be changed in a restricted way via state transition methods like invalidate().



What would be the RESTful way to create Passport resources? A POST to /passports including the state property and check on the server-side that the state is either REGULAR or PRELIMINARY and return a BAD REQUEST response if it is an illegal state? Or two different URLs for creating passport resources, one for "regular" and one for "preliminary" passports?










share|improve this question













How would you design a RESTful API for a class that uses the factory method pattern to create objects?



Let's say you have class Passport:



public class Passport {
final String firstName;
final String lastName;
final Date birthDate;
State state;

private Passport(State state, String firstName, String lastName, Date birthDate) {
this.state = state;
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
}


public static Passport createPreliminary(String firstName, String lastName, Date birthDate) {
return new Passport(PRELIMINARY, firstName, lastName, birthDate);
}

public static Passport createRegular(String firstName, String lastName, Date birthDate) {
return new Passport(REGULAR, firstName, lastName, birthDate);
}

public void invalidate() {
state = INVALID;
}
}

public enum State {
PRELIMINARY,
REGULAR,
EXPIRED,
INVALID
}


A Passport instance can be created in two different states. After creation only the state property can be changed in a restricted way via state transition methods like invalidate().



What would be the RESTful way to create Passport resources? A POST to /passports including the state property and check on the server-side that the state is either REGULAR or PRELIMINARY and return a BAD REQUEST response if it is an illegal state? Or two different URLs for creating passport resources, one for "regular" and one for "preliminary" passports?







rest design-patterns






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 9:50









Christof Schablinski

83




83












  • How would you achive your needs on the Web? You'd ask the server for a resource that teaches you how a request has to be build. The server would respond with a form that offers a client a skeletton what fields and what types are necessary and also provides a convenient submit button to actually perform the request. Whether you create a REGULAR or a PRELIMINARY passport will be determined by probably a dropdown choice. The same can be applied for any REST application as well. Either reuse HTML forms or specify your own general-purpose media-types in that particular case
    – Roman Vottner
    Nov 19 at 18:17


















  • How would you achive your needs on the Web? You'd ask the server for a resource that teaches you how a request has to be build. The server would respond with a form that offers a client a skeletton what fields and what types are necessary and also provides a convenient submit button to actually perform the request. Whether you create a REGULAR or a PRELIMINARY passport will be determined by probably a dropdown choice. The same can be applied for any REST application as well. Either reuse HTML forms or specify your own general-purpose media-types in that particular case
    – Roman Vottner
    Nov 19 at 18:17
















How would you achive your needs on the Web? You'd ask the server for a resource that teaches you how a request has to be build. The server would respond with a form that offers a client a skeletton what fields and what types are necessary and also provides a convenient submit button to actually perform the request. Whether you create a REGULAR or a PRELIMINARY passport will be determined by probably a dropdown choice. The same can be applied for any REST application as well. Either reuse HTML forms or specify your own general-purpose media-types in that particular case
– Roman Vottner
Nov 19 at 18:17




How would you achive your needs on the Web? You'd ask the server for a resource that teaches you how a request has to be build. The server would respond with a form that offers a client a skeletton what fields and what types are necessary and also provides a convenient submit button to actually perform the request. Whether you create a REGULAR or a PRELIMINARY passport will be determined by probably a dropdown choice. The same can be applied for any REST application as well. Either reuse HTML forms or specify your own general-purpose media-types in that particular case
– Roman Vottner
Nov 19 at 18:17












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted











A POST to /passports including the state property and check on the server-side that the state is either REGULAR or PRELIMINARY and return a BAD REQUEST response if it is an illegal state?




This is the option I'd go for. Having one endpoint for adding to the passports resource is the simplest and the easiest to understand.



If you used the other option, every time you added/changed an item to the "State" enum, you'd have to also change your resource structure. It just introduces unnecessary complexity and causes your API design to be less flexible.



Your API should be an abstraction of your internal business logic. Don't make it complex and layered if you don't have to.






share|improve this answer























  • +1 for "Your API should be an abstraction of your internal business logic. Don't make it complex and layered if you don't have to."
    – Eric Stein
    Nov 19 at 14:20











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',
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%2f53372025%2frestful-implementation-of-factory-method-design-pattern%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted











A POST to /passports including the state property and check on the server-side that the state is either REGULAR or PRELIMINARY and return a BAD REQUEST response if it is an illegal state?




This is the option I'd go for. Having one endpoint for adding to the passports resource is the simplest and the easiest to understand.



If you used the other option, every time you added/changed an item to the "State" enum, you'd have to also change your resource structure. It just introduces unnecessary complexity and causes your API design to be less flexible.



Your API should be an abstraction of your internal business logic. Don't make it complex and layered if you don't have to.






share|improve this answer























  • +1 for "Your API should be an abstraction of your internal business logic. Don't make it complex and layered if you don't have to."
    – Eric Stein
    Nov 19 at 14:20















up vote
1
down vote



accepted











A POST to /passports including the state property and check on the server-side that the state is either REGULAR or PRELIMINARY and return a BAD REQUEST response if it is an illegal state?




This is the option I'd go for. Having one endpoint for adding to the passports resource is the simplest and the easiest to understand.



If you used the other option, every time you added/changed an item to the "State" enum, you'd have to also change your resource structure. It just introduces unnecessary complexity and causes your API design to be less flexible.



Your API should be an abstraction of your internal business logic. Don't make it complex and layered if you don't have to.






share|improve this answer























  • +1 for "Your API should be an abstraction of your internal business logic. Don't make it complex and layered if you don't have to."
    – Eric Stein
    Nov 19 at 14:20













up vote
1
down vote



accepted







up vote
1
down vote



accepted







A POST to /passports including the state property and check on the server-side that the state is either REGULAR or PRELIMINARY and return a BAD REQUEST response if it is an illegal state?




This is the option I'd go for. Having one endpoint for adding to the passports resource is the simplest and the easiest to understand.



If you used the other option, every time you added/changed an item to the "State" enum, you'd have to also change your resource structure. It just introduces unnecessary complexity and causes your API design to be less flexible.



Your API should be an abstraction of your internal business logic. Don't make it complex and layered if you don't have to.






share|improve this answer















A POST to /passports including the state property and check on the server-side that the state is either REGULAR or PRELIMINARY and return a BAD REQUEST response if it is an illegal state?




This is the option I'd go for. Having one endpoint for adding to the passports resource is the simplest and the easiest to understand.



If you used the other option, every time you added/changed an item to the "State" enum, you'd have to also change your resource structure. It just introduces unnecessary complexity and causes your API design to be less flexible.



Your API should be an abstraction of your internal business logic. Don't make it complex and layered if you don't have to.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 at 12:34

























answered Nov 19 at 12:15









J Marlow

1055




1055












  • +1 for "Your API should be an abstraction of your internal business logic. Don't make it complex and layered if you don't have to."
    – Eric Stein
    Nov 19 at 14:20


















  • +1 for "Your API should be an abstraction of your internal business logic. Don't make it complex and layered if you don't have to."
    – Eric Stein
    Nov 19 at 14:20
















+1 for "Your API should be an abstraction of your internal business logic. Don't make it complex and layered if you don't have to."
– Eric Stein
Nov 19 at 14:20




+1 for "Your API should be an abstraction of your internal business logic. Don't make it complex and layered if you don't have to."
– Eric Stein
Nov 19 at 14:20


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53372025%2frestful-implementation-of-factory-method-design-pattern%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

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?