Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass enumerated values to a web service

Tags:

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?

like image 626
Edo Avatar asked Aug 04 '08 23:08

Edo


People also ask

How do you pass enum in Request Param?

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) { // ... }

What is enumeration in WSDL?

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.

How do you use enum in DTO?

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; } ... }


2 Answers

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.

like image 133
Peter Burns Avatar answered Sep 21 '22 04:09

Peter Burns


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); } 
like image 32
lubos hasko Avatar answered Sep 21 '22 04:09

lubos hasko