Parag Bhingre
Parag Bhingre

Reputation: 850

how to send parameters through link to java web service and how to get it in java

I am trying to implement one program where i want to get parameters which are sent by a link. But i am not getting how to create link so that it will contain parameters and how that parameters i should access in web service of java.

I have done this.

http://localhost:8080/RestWebService/rest/person/todo/

this is my actual link without parameters and todo is my function which is returning person json object.

@GET
    @Path("todo")
    @Produces(MediaType.APPLICATION_JSON)
    public Person whatEverNameYouLike(@PathParam("varX") String varX,@PathParam("varY") String varY) {
        Person todo = new Person();
            todo.setEmail(varX);
            todo.setFirstName(varX);
            todo.setId(1);
            todo.setLastName(varX);
            return todo;
    }

this is my function in java in which i want to access data which is coming from link which is given above.

http://localhost:8080/RestWebService/rest/person/todo/bcd/asd/1/asd

i tried giving parameters after todo like given in above link it wont worked.

<?xml version="1.0" encoding="UTF-8"?>
<web-app
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID"
    version="3.0">
  <display-name>JerseyRESTServer</display-name>
  <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.avilyne.rest.resource</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

Upvotes: 0

Views: 147

Answers (2)

Abdelhak
Abdelhak

Reputation: 8387

If you have @path("/person") in your class controller, add a backslash like this @Path("/todo")in your method and try to use this url:

http://localhost:8080/RestWebService/rest/person/todo/varX/varY

Otherwise use this:

 http://localhost:8080/RestWebService/rest/todo/varX/varY

But in your controller try to specify the param like this:

@Path("todo/{varX}/{varY}")

Upvotes: 1

Albert Bos
Albert Bos

Reputation: 2062

In order to make your @PathParam work you need to update the @Path as well.

@Path("todo/{varX}/{varY}")

Upvotes: 2

Related Questions