0

I have the following method in a REST api I am developing:

@POST
@Consumes (MediaType.APPLICATION_JSON+ ";charset=utf-8")
@Produces (MediaType.APPLICATION_JSON + ";charset=utf-8")
public RunInstance startRun ( RunInstance runInstance ) 

RunInstance object works like a holder for various properties that may or may not be required before a long-running operation can be started. Some of its fields will be used as feedback later, where this object will be returned when a client needs an operation status update:

public class RunInstance {
    private int ruleId; 
    @JsonbTransient private String ruleSubmitResult; 
    private String status; 
    private String errorMessage; 
    private String action;
    private int runId; 
    private java.time.LocalDateTime startDt ; 
    private java.time.LocalDateTime endDt ; 
    private int inputRows;
    private int currentLoop;
    ....

Now, the actual operation that will be carried out will differ based on the ruleId. Each operation has different requirements, and can accept different parameters.

If I keep my method signature as

public RunInstance startRun ( RunInstance runInstance )

It allows me to automatically fill this object. However the client will also pass some parameters/json data that pertain to each specific operation and will differ depending on the type of operation.

What I want to do is instead of changing the method signature to something like

@POST
    @Consumes (MediaType.APPLICATION_JSON+ ";charset=utf-8")
    @Produces (MediaType.APPLICATION_JSON + ";charset=utf-8")
    public RunInstance startRun ( Map<String,String> bodyParams) 

then have to manually extract params related to fill my RunInstance object, if I can have it filled automatically while also having access to the rest of the params (that where not used as values for this object).

However, according to this post it is not possible to have two objects posted: JAX-RS Post multiple objects

Does anyone know of any way to achieve something like this? i.e. like

public RunInstance startRun ( RunInstance runInstance , Map<String,String> remainingbodyParams)
Kinnison84
  • 176
  • 1
  • 8
  • 1
    What about putting the map as a property of the RunInstance? I Know Jackson can do that. I don't know about JSONB though. – Paul Samsotha Feb 12 '20 at 02:39
  • I tried putting the map as a property, and although it does not work as I described it solves the problem well enough: the client can send a named json object that corersponds to the name of the Map, containing sets of name-value pairs that get passed to the Map. Then I can use those as the parameters needed for the application's purposes. – Kinnison84 Feb 12 '20 at 08:52

0 Answers0