user1757189
user1757189

Reputation: 65

Passing parameter to a Jersey ressource

How can I retrieve the mapProp from TestRes resource ?

I'm using jersey embedded in jetty.

    Map<String,Object> mapProp= new HashMap<String,Object>()
    mapProp.put("message","HelloWorld")

    URI baseUri = UriBuilder.fromUri("http://localhost/").port(9998).build();
    ResourceConfig config = new ResourceConfig(TestRes.class);
    config.addProperties(mapProp)
    Server server = JettyHttpContainerFactory.createServer(baseUri, config);

Thanks

Upvotes: 2

Views: 1422

Answers (2)

Paul Samsotha
Paul Samsotha

Reputation: 209112

You can inject a javax.ws.rs.core.Configuration in your resource class with @Context annotation. With the Configuration, you can call getProperties() to get the properties map that you set in the ResourceConfig

Here's a complete example

import java.util.*;
import javax.ws.rs.*;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.*;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;

public class TestConfiguration extends JerseyTest {

    @Path("/configuration")
    public static class ConfigurationResource {

        @Context
        Configuration config;

        @GET
        @Produces(MediaType.TEXT_PLAIN)
        public Response getConfig() {
            StringBuilder builder = new StringBuilder("============\n");
            Map<String, Object> props = config.getProperties();

            for (Map.Entry<String, Object> entry : props.entrySet()) {
                builder.append(entry.getKey()).append(" : ")
                        .append(entry.getValue()).append("\n");
            }
            builder.append("=============");
            return Response.ok(builder.toString()).build();
        }
    }

    @Override
    public Application configure() {
        Map<String, Object> mapProp = new HashMap<String, Object>();
        mapProp.put("message", "HelloWorld");
        return new ResourceConfig(ConfigurationResource.class)
                .setProperties(mapProp);
    }

    @Test
    public void testGetConfiguration() {
        String response = ClientBuilder.newClient()
                .target("http://localhost:9998/configuration")
                .request(MediaType.TEXT_PLAIN)
                .get(String.class);
        System.out.println(response);
    }
}

This is the only dependency you need to run this test:

    <dependency>
        <groupId>org.glassfish.jersey.test-framework.providers</groupId>
        <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
        <version>2.13</version>
    </dependency>

You should see this as the result

============
message : HelloWorld
=============

Upvotes: 2

questionaire
questionaire

Reputation: 2585

Actually some more code of your TestResclass would be useful.

But I guess, what you are looking for is @PathParam.

For example:

@Path("yourPath/{map})
public void getMyMap(@PathParam("map")String map){
//Do something
}

Will handover the parameter map.

Also well explained here

Upvotes: 1

Related Questions