【问题标题】:Passing parameter to a Jersey ressource将参数传递给 Jersey 资源
【发布时间】:2014-12-11 20:39:01
【问题描述】:

如何从 TestRes 资源中检索 mapProp ?

我正在使用嵌入码头的运动衫。

    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);

谢谢

【问题讨论】:

    标签: java rest jersey jetty jax-rs


    【解决方案1】:

    您可以使用@Context 注释在资源类中注入javax.ws.rs.core.Configuration。使用Configuration,您可以调用getProperties()获取您在ResourceConfig中设置的属性映射

    这是一个完整的例子

    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);
        }
    }
    

    这是运行此测试所需的唯一依赖项:

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

    你应该看到这个结果

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

    【讨论】:

      【解决方案2】:

      实际上,您的TestResclass 的更多代码会很有用。

      但我猜,你要找的是@PathParam。

      例如:

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

      将移交参数图。

      也很好解释here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-25
        相关资源
        最近更新 更多