0

I have a maven java web application developed using Netbeans. I have figured out to run a parameter based Restful service successfully.

The URL contains three names in separated by slashes before providing the parameter.

http://localhost:8080/chims/api/data/list?name=district_list

Can I have a URL with less slashes like

http://localhost:8080/chims/data?name=district_list

This is the applicatoin config file.

package org.netbeans.rest.application.config;

import javax.ws.rs.core.Application;

@javax.ws.rs.ApplicationPath("api")
public class ApplicationConfig extends Application {


  
   
    
}

This is the service file.

package lk.gov.health.phsp.ws;

import java.util.List;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Produces;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import lk.gov.health.phsp.bean.AreaApplicationController;
import lk.gov.health.phsp.entity.Area;
import lk.gov.health.phsp.enums.AreaType;
import org.json.JSONArray;
import org.json.JSONObject;

@Path("data")
@RequestScoped
public class ApiResource {

    @Context
    private UriInfo context;

    @Inject
    AreaApplicationController areaApplicationController;

    /**
     * Creates a new instance of GenericResource
     */
    public ApiResource() {
    }

    @GET
    @Path("list")
    @Produces(MediaType.APPLICATION_JSON)
    public String getJson(@QueryParam("name") String name) {
        JSONObject jSONObjectOut;
        if (name == null || name.trim().equals("")) {
            jSONObjectOut = errorMessageInstruction();
        } else {
            switch (name) {
                case "district_list":
                    jSONObjectOut = districtList();
                    break;
                default:
                    jSONObjectOut = errorMessage();
            }
        }
        String json = jSONObjectOut.toString();
        return json;
    }

    private JSONObject districtList() {
        JSONObject jSONObjectOut = new JSONObject();
        JSONArray array = new JSONArray();
        List<Area> ds = areaApplicationController.getAllAreas(AreaType.District);
        for (Area a : ds) {
            JSONObject ja = new JSONObject();
            ja.put("district_id", a.getCode());
            ja.put("district_name", a.getName());
            array.put(ja);
        }
        jSONObjectOut.put("data", array);
        jSONObjectOut.put("status", successMessage());
        return jSONObjectOut;
    }

    private JSONObject successMessage() {
        JSONObject jSONObjectOut = new JSONObject();
        jSONObjectOut.put("code", 200);
        jSONObjectOut.put("type", "success");
        return jSONObjectOut;
    }

    private JSONObject errorMessage() {
        JSONObject jSONObjectOut = new JSONObject();
        jSONObjectOut.put("code", 400);
        jSONObjectOut.put("type", "error");
        jSONObjectOut.put("message", "Parameter name is not recognized.");
        return jSONObjectOut;
    }
    
    
    private JSONObject errorMessageInstruction() {
        JSONObject jSONObjectOut = new JSONObject();
        jSONObjectOut.put("code", 400);
        jSONObjectOut.put("type", "error");
        jSONObjectOut.put("message", "You must provide a value for the parameter name.");
        return jSONObjectOut;
    }

}

I have not done any changes to the web.xml file. All the tutorials I went through did not give me a clear picture as to how and why I have to change it. Even without changing it, the web service works as expected.

How can I reduce the slashes in the URL?

Buddhika Ariyaratne
  • 2,339
  • 6
  • 51
  • 88
  • 1
    Make `"api"` a blank string `""` or `"/*"`. And remove the `@Path("list")`. – Paul Samsotha Mar 02 '21 at 17:40
  • I could remove @Path("list"). But when I changed "api" to "", the JSF application is not running. Still your comment allowed me to reduce one slash. Only one more is needed. Thank you – Buddhika Ariyaratne Mar 02 '21 at 22:24
  • 1
    The only way then is to configure your Jersey application to run as a servlet filter instead of a servlet. This way it can forward unknown requests to other servlets (i.e. JSF) down the line. See [this post](https://stackoverflow.com/q/12422660/2587435) – Paul Samsotha Mar 02 '21 at 22:27
  • 1
    Just curious, but why do you care so much to remove just a few characters from the URL? – Paul Samsotha Mar 02 '21 at 22:28
  • That is working perfectly. If you just copy and pasted two comments as the answer, I can accept it. Thank you so much. – Buddhika Ariyaratne Mar 02 '21 at 22:40
  • I was given the URL https://www.example.org/data?name=district_list to develop the API from the other API client developer team. – Buddhika Ariyaratne Mar 02 '21 at 22:42

1 Answers1

1

The first thing you can do is remove the @Path("list"). A GET to /data will automatically go to the getJson method.

The next thing you can do is remove the api. You can do this by changing the "api" to "", "/", or "/*". All three will result in the same "/*". What happens when you do this is that Jersey will now take all requests that come to the server (and the same context). Any other servlets or static content will not be reachable.

To get around this, you can configure Jersey to run as a servlet filter instead of a servlet. Then configure it to forward unknown requests. See this post for how to configure this.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720