Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jaxrs-dev] @PathParam not picking value ?

Is this the correct way to use  jaxrs API.



**
 *
 *   child to RootContext julian
 *   Application Path Rest Web Service
 *
 */
@javax.ws.rs.ApplicationPath("/resources")
public class ApplicationRestService  extends javax.ws.rs.core.Application {

}

/*
 *   Math REST service, child resource to parent ApplicationPath
 *
 */

import javax.ws.rs.*;

@Path("MathResource")
public class MathRestService {

// curl -X GET
// http://localhost:8080/julian/resources/MathResource/addition/12/3

@GET
@Produces("text/plain")
  @Path("addition/{PP_firstNum}/{PP_secondNum}")
    public String add( @PathParam("PP_firstNum") String infirst,
      @PathParam("PP_secondNum") int inSecond )
{
// take second character  
int inTemp = Integer.parseInt(infirst.substring(1,2));
return "" + (inTemp + inSecond); // answer should be 5
} // add

} // class

On Tue, 26 Jan 2021, 10:52 Som Lima, <somplasticllc@xxxxxxxxx> wrote:
I found an example of  {inNum :.*} ,  all my queries has been solved. 

Thank you. 



On Tue, 26 Jan 2021, 09:38 Som Lima, <somplasticllc@xxxxxxxxx> wrote:
I have rectified the error with incorrect library import
and divided  the code as suggested which does make sense moreover  it seems like a best practice.

Can you please explain what is the difference between these two different syntax arguments and what is the benefit if any ?
They both of these two lines of code work.
  @javax.ws.rs.Path("addition/{inNum : .*}")
// @javax.ws.rs.Path("addition/{inNum}")

 
package jakarta.julian;
/**
 *
 *  Application Path Rest Web Service
 *
 */

@javax.ws.rs.ApplicationPath("/resources")
 public class Julian  extends javax.ws.rs.core.Application {
}



package jakarta.julian;

/* *  Addition REST service, child resource to parent ApplicationPath  
 *   
 */

@javax.ws.rs.Path("AdditionRestService")
public class AdditionResource {

// curl -X GET
// http://localhost:8080/julian/resources/AdditionRestService/addition/0123

      @javax.ws.rs.GET
     @javax.ws.rs.Produces("text/plain")
     @javax.ws.rs.Path("addition/{inNum : .*}")
  // @javax.ws.rs.Path("addition/{inNum}")

    public String add(
            @javax.ws.rs.PathParam("inNum") String inNum) {
            // get second character ignore rest
           int intemp = Integer.parseInt(inNum.substring(1, 2));
           return "" + (intemp + 3) ;    
    } // add

} // class

On Tue, 26 Jan 2021, 08:18 Edwin Amoakwa, <edwin.amoakwa@xxxxxxxxx> wrote:
Also, check the PathParam you are using. 

It should be : 
import javax.ws.rs.PathParam;
Not the one from the websocket.

Thanks,
Edwin.

On Tue, 26 Jan 2021, 8:08 am Ivar Grimstad, <ivar.grimstad@xxxxxxxxxxxxxxxxxxxxxx> wrote:
I don't think you can have your resource methods in the Application class.
You should move your resource to a separate class. E.g.:

@Path("MyRestService")
public class ConvertResource {
@GET
@Produces("text/plain")
@Path("convert/{inNum : .*}")
    public String add(
            @PathParam("inNum") String inNum) {

   return ""+ inNum;
   
      }
}

And:

@ApplicationPath("/resources")
public class Julian  extends Application {
}

That should work.

On Tue, Jan 26, 2021 at 8:01 AM Som Lima <somplasticllc@xxxxxxxxx> wrote:
return value is blank in this simplified program to pass a value .

What is the issue  ?


import javax.websocket.server.
PathParam;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Application;
/**
 *
 * Rest Web Service
 *
 */
@ApplicationPath("/resources")
@Path("MyRestService")
public class Julian  extends Application {

// curl -X GET
// http://localhost:8080/julian/resources/MyRestService/convert/0123
@GET
@Produces("text/plain")
@Path("convert/{inNum : .*}")
    public String add(
            @PathParam("inNum") String inNum) {

   return ""+ inNum;
   
      }
}

_______________________________________________
jaxrs-dev mailing list
jaxrs-dev@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jaxrs-dev


--

Ivar Grimstad

Jakarta EE Developer Advocate | Eclipse Foundation

Eclipse Foundation - Community. Code. Collaboration. 

_______________________________________________
jaxrs-dev mailing list
jaxrs-dev@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jaxrs-dev
_______________________________________________
jaxrs-dev mailing list
jaxrs-dev@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jaxrs-dev

Back to the top