My dilemma is, basically, how to share an enumeration between two applications.
The users upload documents through a front-end application that is on the web. This application calls a web service of the back-end application and passes the document to it. The back-end app saves the document and inserts a row in the Document table.
The document type (7 possible document types: Invoice, Contract etc.) is passed as a parameter to the web service's UploadDocument method. The question is, what should the type (and possible values) of this parameter be?
Since you need to hardcode these values in both applications, I think it is O.K. to use a descriptive string (Invoice, Contract, WorkOrder, SignedWorkOrder).
Is it maybe a better approach to create a DocumentTypes enumeration in the first application, and to reproduce it also in the second application, and then pass the corresponding integer value to the web service between them?
We can then use this enum as a RequestParameter in a Spring controller: @GetMapping("/mode2str") public String getStringToMode(@RequestParam("mode") Modes mode) { // ... } Or we can use it as a PathVariable: @GetMapping("/findbymode/{mode}") public String findByEnum(@PathVariable("mode") Modes mode) { // ... }
WSDL uses XML Schema to describe the shape of data. XML Schema has no concept equivalent to an enum in a programming language. In most programming languages I know, an enum is a named constant. The enumeration facet in XML Schema does not describe named values.
You can use the name() or toString() method of your Enum . You can provide an additional "translation" inside the Enum like: public enum Status { S1("translation1"), S2("translation2"), S3("translation3"); private final String translation; private Status(String t) { translation = t; } ... }
I'd suggest against passing an integer between them, simply for purposes of readability and debugging. Say you're going through your logs and you see a bunch of 500 errors for DocumentType=4. Now you've got to go look up which DocumentType is 4. Or if one of the applications refers to a number that doesn't exist in the other, perhaps due to mismatched versions.
It's a bit more code, and it rubs the static typing part of the brain a bit raw, but in protocols on top of HTTP the received wisdom is to side with legible strings over opaque enumerations.
I would still use enumeration internally but would expect consumers to pass me only the name, not the numeric value itself.
just some silly example to illustrate:
public enum DocumentType { Invoice, Contract, WorkOrder, SignedWorkOrder } [WebMethod] public void UploadDocument(string type, byte[] data) { DocumentType docType = (DocumentType)Enum.Parse(typeof(DocumentType), type); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With