I want to define API method arguments and return values abstractly

Asked 2 years ago, Updated 2 years ago, 112 views

(Windows, Java 1.8.0)
Currently, I am writing the following code as an API using JAX-RS (Jersey).

@Path("API_01")//URL at API call
    
    @POST
    @Produce(MediaType.APPLICATION_JSON)
    publicResponseObj execute(RequestObj request){
        
        ResponseObj response=new ResponseObj();
        response.status="00";
        response.message="";
        
        return response;
    }
 *When you submit a POST request, Jersey automatically calls the execute method above.
JJersey converts the request contents in JSON format to RequestObj.
Re RequestObj, ResponseObj are simple VO with scalar member variables.

Regarding the above, I would like to do the following.
 ①Create a base class for a class describing API processing, and
in the execute method for the base class   Want to describe common actions
  (There are multiple APIs that you actually create, so you want to write their common actions in the base class.)
  (After describing common processing, an abstract method such as executeSub(...) is called
Assume to take shape)
  
 ②Class describing API processing overrides executeSub() above, and individual processing should be described there
  
 ③Each request object and response object has a common variable across multiple APIs, so I would like to have the base class of the request object and the base class of the response object
   Example Request Object Base Class: RequestObjBase
      Base class of response objects:ResponseObjBase
      
      Request Object Request1 extends RequestObjBase
for API1           Response Object Response1 extensions ResponseObjBase
     
I don't know how to describe it here.
 ·How to describe the argument and return type of the execute method of the base class
  (I would like to make it a generic description of all API requests and response types to be implemented this time.)
  
 ·When the method definition can handle the type of request and response collectively, it is Override
  How can I write within the methods of this class to receive a Request Type 1 object?
  
I would appreciate it if you could tell me how to describe it above and what to do with the proposed implementation.

試み In my attempt, I specified the method arguments and return values of the base class in the object base class for request response, but I gave up because I could not downcast the contents of the request and the type of return value was inconsistent compilation error.
クラス I felt that it could be implemented using Class<?extendsRequestObjBase> and generics to treat class types abstractly/symmetrically, so I didn't know how to describe it for this purpose.
メソI was able to force HashMap to cast the method argument and return value as Object, but due to the complexity of the description, I would like to write the implementation using the above types such as Request1, Response1, etc.

java api generics

2022-09-30 19:27

1 Answers

You can limit the types available with boundary parameters such as <RESP extensionsResponseObjBase>.

Base class:

public abstract class MyAbstractResource<RESP extensionsResponseObjBase, REQ extensions RequestObjBase>{

    @POST
    @Produce(MediaType.APPLICATION_JSON)
    public RESP execute(final REQ request) {

        // ...
        // common processing execution

        return executeSub(request);
    }

    protected abstract RESP executeSub(REQ request);

}

Physical resource class:

@Path("/example1")
public class MyConcreteResource extensions MyAbstractResource<Response1,Request1>{

    @ Override
    protectedResponse1 executeSub(finalRequest1request){

        final Response 1 response = new Response 1();
        response.status="00";
        response.message="";

        return response;
    }
}


2022-09-30 19:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.