Junos Space SDK > Developer Guides > Junos Space Application Developer Guide > Programming with the Junos Space SDK > Developing Junos Space Applications > Creating EJBs

Guidelines for RESTification of EJB Methods

The EJB-REST wizard can be used to expose EJB Methods as RESTful services. For this, the EJB method needs to follow certain guidelines so that these can be RESTified. If the EJB method signature does not follow these guidelines, the EJB-REST wizard might not be able to RESTify them correctly. To resolve such issues, change the method signature to a compatible type or RESTify it manually.

Note: All primitive types, wrapper classes such as Integer, Float, String and enum types are considered non-Complex types.

The following are the set of guidelines for each REST method type.

GET Method

The EJB methods that you wish to expose as GET REST services must conform to the following signatures:

  1. Method returning a complex type:

    public Country getCountry(id);

  2. Methods returning primitive type or String or enum:

    public String getUserName();

    In this case, a wrapper DTO is generated that has one String field and a setter and getter.

  3. The return type can be a parameterized collection or its derived classes or interface. For example:

    Collection<User> getUsers();
    List<User> getAllUsers();

  4. A parameterized collection of String:

    Collection<String> getUserNames();
    PagingContext<String> getUserNames();

Unsupported signatures include:

  1. Unparameterized lists and collections:

    Collection getUsers();
    List getAllUsers();
    Set getAllUsers();
    Object[] getAllUsers();

  2. Methods returning an array of primitive type, wrapper type, and string:

    int [] getAllPopulations();

POST Method

The EJB method that you wish to expose as a POST REST services must conform to the following signatures:

  1. POST methods should have at least one Complex type as an argument, except in the case of envelope (where only a primitive type added as an HTTP request body parameter constructs an envelope DTO).

    public State addState(State state)

  2. Apart from one complex type, the POST method can additionally have primitive types or a string as PathParams or QueryParams.

    i>public State addStateToCountry(State state, int countryId)

  3. The return type can be void or boolean or any primitive type.

    public void addState(State state)

  4. The POST method can have more than one complex type as an argument. In this case, the return type should be void.

    public void addState(State state, Location location)

    In this case, the resulting REST DTO will be an envelope DTO, combining both State and Location.

Limitations

Argument types of array or map are not supported. That is, the EJB-REST wizard cannot create REST services for these methods. In this case, you need to create a facade EJB method which converts from Map to List.

public void addStates(Map<String, State> states)

PUT Method

The EJB method you wish to expose as a PUT REST service must conform to the following signatures:

  1. PUT methods should have at least one complex type as an argument, except in the case of envelope (where only the primitive type added as an HTTP request body parameter constructs the envelope DTO).

    public State updateState(State state)

  2. Apart from one complex type, the PUT method can additionally have primitive types or string as PathParams or QueryParams.

    public State updateStateWithinCountry(State state, int countryId)

  3. The return type can be void or boolean or any primitive type.


    public void updateState(State state)
  4. The PUT method can have more than one complex type as an argument. In this case, the return type should be void.

    public void updateState(State state, Location location)

    In this case, the resulting REST DTO will be an envelope DTO, combining both State and Location.

Limitations

Argument types of array or map are not supported. That is, the EJB-REST wizard cannot create REST services for these methods. In this case, you need to create a facade EJB method which converts from Map to List.

public void updateStates(Map<String, State> states)

DELETE Method

The EJB methods you wish to expose as a DELETE REST services must conform to the following signatures:

  1. The EJB method can have primitive or String arguments that should be PathParams or QueryParams.

    public void deleteState(int stateId) throws Exception;

  2. For deletion, the EJB method should have a return type of void. A delete request should return no content (HTTP status 200).

    public void deleteState(int id) throws Exception

Note: Media types are not supported with DELETE methods, because the DELETE method doesn't have a request or a response body. Therefore, by convention, DELETE methods are only limited to CRUD methods. For example, do not use a DELETE method to cancel a job or perform an operation other than deleting an object from a collection. DELETE methods cannot be versioned.