0

I have a class with my getters and setters, containing values, for example:

String value1;
String value2;
double result;

I want to use these two strings to determine what should happen with the result. If value1 equals "one" and value2 = "two" then the result should be multiplied by a predefined value.

@GET
@Path("/{value1}/{value2}/{result}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public double getResult() {
    Something mon = new Something();
    mon.setOne(22.2);
    mon.setTwo(11.1);
    if("/{value1}".equals("one")){
       //multiply by mon.setOne;
    }
    return 0;
   
}

How do I read and access the values defined in the path?

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
okmijn
  • 13
  • 3

2 Answers2

0

If you want to receive the values as path parameters (like /foo/one/two/something), you can use @PathParam:

@GET
@Path("/foo/{value1}/{value2}/{result}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public double getResult(@PathParam("value1") String value1, 
                        @PathParam("value2") String value2,
                        @PathParam("result") String result) {
   ...
}

But, depending on what you intend to do, you could consider using query parameters (like /foo?value1=one&value2=two&result=something) with @QueryParam:

@GET
@Path("/foo")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public double getResult(@QueryParam("value1") String value1, 
                        @QueryParam("value2") String value2,
                        @QueryParam("result") String result) {
   ...
}

You may want to check the answers to this question for details on when to use each of them.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
-1

First of all you should use a unique API name I used /GetResult then put the parameter.

@GET
@Path("/GetResult/{value1}/{value2}/{result}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public double getResult(
            @PathParam(value = "value1") String value1, 
            @PathParam(value = "value2") String value2,
            @PathParam(value = "result") String result) {

        if (value1.equals("one")) {

        }

        return 0;

    }