简体   繁体   中英

Using queryParam linked or unlinked with WebTarget DELETE produces different results

I have a basic javax.ws.rs REST API class:

@Path("/")
public class DeleteApi {
    @DELETE
    public void testDelete(@DefaultValue("false") @QueryParam("id") boolean id) {
        logger.info("id [{}]", id);
    }
}

I'm using a javax.ws.rs.client.Client within a test to test this DELETE method:

@Test
public void testCallDelete() {
    // Not linked version
    WebTarget target = client.target("http://127.0.0.1:8080/fscrawler/");
    target.queryParam("id", true);
    target.request().delete();

    // Linked version
    client.target("http://127.0.0.1:8080/fscrawler/")
            .queryParam("id", true)
            .request()
            .delete();
}

It produces:

23:37:14,870 INFO  [f.p.e.c.f.r.RestApi] id [false]
23:37:14,910 INFO  [f.p.e.c.f.r.RestApi] id [true]

In case it's helpful here is my main dependencies in my pom.xml file:

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-grizzly2-http</artifactId>
        <version>2.25.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.25.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.25.1</version>
    </dependency>

I know I'm doing something probably stupid but still can see what is my error here. Any help would be greatly appreciated.

Thanks!

The line

target.queryParam("id", true);

Doesn't modify target , it just returns a new WebTarget with the queryparam set

You could do

target = target.queryParam("id", true);

but your second test is the correct way

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM