1

I am using jersey to create a web service . I do have a delete method in REST API, and I need to pass an ArrayList from Jersey Client to the REST API. In there , my ArrayList type is DrugNamesBean .

I have tried in this way but I have no idea about this.

Client client = ClientBuilder.newClient();          

List<DrugNamesBean> drugNamesBeans=new ArrayList<DrugNamesBean>();
DrugNamesBean bean1=new DrugNamesBean();
bean1.setIdDrugName(23974);

DrugNamesBean bean2=new DrugNamesBean();
bean2.setIdDrugName(23975);

DrugNamesBean bean3=new DrugNamesBean();
bean3.setIdDrugName(23976);

drugNamesBeans.add(bean1);
drugNamesBeans.add(bean2);
drugNamesBeans.add(bean3);  

WebTarget webTarget=client.target("http://localhost:8080/Rest/rest/drug_names")                
            .path("/deleteDrugNames").queryParam("list", drugNamesBeans);

String delete = webTarget.request(MediaType.APPLICATION_JSON_TYPE).delete(new GenericType<String>(){});

System.out.println("Deleted - " + delete); 

Below is my REST API method

@DELETE
    @Path("/deleteDrugNames")
    public String deleteDrugNames(@QueryParam("list")List<DrugNamesBean> drugNamesBeans){

        DrugNamesInterface drugNamesInterface=new DrugNamesTable();
        String deleteDrugNames = drugNamesInterface.deleteDrugNames(drugNamesBeans);
        return deleteDrugNames;
    }

Have you any ideas about this ?

UPDATE

I could do above using jersey post. Like below,

    Client client = ClientBuilder.newClient();          

    List<DrugNamesBean> drugNamesBeans=new ArrayList<DrugNamesBean>();
    DrugNamesBean bean1=new DrugNamesBean();
    bean1.setIdDrugName(23977);

    DrugNamesBean bean2=new DrugNamesBean();
    bean2.setIdDrugName(23978);

    DrugNamesBean bean3=new DrugNamesBean();
    bean3.setIdDrugName(23979);

    drugNamesBeans.add(bean1);
    drugNamesBeans.add(bean2);
    drugNamesBeans.add(bean3);  

    WebTarget webTarget=client.target("http://localhost:8080/Rest/rest/drug_names")                
            .path("/deleteDrugNames");

    webTarget.request(MediaType.APPLICATION_JSON_TYPE)
                .post(Entity.entity(drugNamesBeans, MediaType.APPLICATION_JSON));

REST API method,

@POST
@Path("/deleteDrugNames")
@Consumes(MediaType.APPLICATION_JSON)
public String deleteDrugNames(List<DrugNamesBean> drugNamesBeans){

    DrugNamesInterface drugNamesInterface=new DrugNamesTable();
    String deleteDrugNames = drugNamesInterface.deleteDrugNames(drugNamesBeans);
    return deleteDrugNames;
}

How could I use jersey delete instead of jersey post?

Terance Wijesuriya
  • 1,928
  • 9
  • 31
  • 61

5 Answers5

2

There are two ways to pass a list that I know of.

@MatrixParam("drugId") List<Long> drugIds)

http://localhost:8080/Rest/rest/drugs?drugId=1;drugId=2;drugId=3;

and

@QueryParam("drugId") List<Long> drugIds

http://localhost:8080/Rest/rest/drugs?drugId=1&drugId=2&drugId=3

From Tim Sylvester's answer here

The important difference is that matrix parameters apply to a particular path element while query parameters apply to the request as a whole.

Community
  • 1
  • 1
GreyGoose
  • 600
  • 4
  • 13
1

In general most of the deletes used to be a deletion of single record with url

http://localhost:8080/Rest/rest/drug_names/{id}

but in your case where multiple record deletes are required then query parameter is the one of the way. when i see your code the bean contains only "durgName"value which is unique value for any drug, so its better to pass the list as comma separated(or any symbol to differentiate the value) list of values in the url.

http://localhost:8080/Rest/rest/drug_names/deleteDrugNames?list=23974,23975

R Rajesh
  • 41
  • 5
1

You should use

@QueryParam("drugId") List<Long> drugIds

for

DELETE http://localhost:8080/Rest/rest/drugs?drugId=1&drugId=2&drugId=3

or single Id

@PathParam("drugId") Long drugId

for

DELETE http://localhost:8080/Rest/rest/drugs/{drugId}

instead of list of beans for passing as QueryParam. It's recommended to remove deleteDrugNames from the path because HTTP DELETE is used so it is already clear that you're removing - you could make similar method HTTP GET http://localhost:8080/Rest/rest/drugs?drugId=1&drugId=2&drugId=3 for retrieving drugs information. Also change drug_names to drugs. I would suggest to avoid passing entity body within HTTP DELETE request because some servers may not support it unless configured - Jersey test - Http Delete method with JSON request

Community
  • 1
  • 1
Justinas Jakavonis
  • 8,220
  • 10
  • 69
  • 114
1

In this example we are going to talk about how you can use @QueryParam annotation to parse URI Query Parameters in a JAX-RS RESTful service.Basically, @QueryParam denotes that the value of the Query Parameter with the corresponding name will be parsed, and if parsed correctly it will be available on the method argument denoted with@QueryParam.There are baically two ways to pass parameters in a GET request in REST services. One of them is URI path parameters. We define this parameter with @Path("/{parameter}") annotation on a method.For example, to pass “Enjoy” as the value of a query parameter called queryparam, one should follow URLs with a pattern like this: .../bbb?queryparam=Enjoy.You can use @Context annotation usually to inject contextual Java types related to the request or response. In a JAX-RS application using servlet, ServletConfig, ServletContext, HttpServletRequest and HttpServletResponse objects are available using @Context.

problem: http://localhost:8080/JAXRS-HelloWorld/rest/helloWorldREST/parameters?parameter2=Examples&parameter2=09709 and you want to collect data from url,so using @Context UriInfo uri object you can directly fetch data in list form.

Tapan
  • 61
  • 1
  • 3
0
<pre>
<code>
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

@Path("/helloWorldREST")
public class HelloWorldREST {

    @GET
    @Path("/parameters")
    public Response responseMsg( @Context UriInfo uriInfo) {

        String parameter1 = uriInfo.getQueryParameters().getFirst("parameter1");

        List<String> parameter2=  uriInfo.getQueryParameters().get("parameter2");

        String output = "Prameter1: " + parameter1 + "\nParameter2: " + parameter2.toString();

        return Response.status(200).entity(output).build(); 
    }
}
</code>
</pre>

Use UriInfo  [QueryParam in Jax-rx][1]
Tapan
  • 61
  • 1
  • 3
  • 1
    Can you also provide a brief explanation of the code? – Stephan Hogenboom Jun 18 '19 at 13:36
  • problem: http://localhost:8080/JAXRS-HelloWorld/rest/helloWorldREST/parameters?parameter2=Examples&parameter2=09709 and you want to collect data from url,so using @Context UriInfo uri object you can directly fetch data in list form. – Tapan Jun 19 '19 at 06:39