简体   繁体   English

如何配置嵌入式jetty来访问Jersey资源?

[英]How to configure embedded jetty to access Jersey resources?

I'm trying to configure embedded jetty to talk to my Jersey resources but I can't figure out how to do it. 我正在尝试配置嵌入式jetty来与我的Jersey资源交谈,但我无法弄清楚如何做到这一点。 I've tried a couple of different things but nothing seems to work. 我尝试过几种不同的东西但似乎没什么用。 The jetty tutorials don't really handle how to do it with Jersey. jetty教程并没有真正处理如何使用Jersey。 Any code suggestions or links are greatly appreciated 任何代码建议或链接都​​非常感谢

EDIT: 编辑:

 package pojo;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.glassfish.jersey.servlet.ServletContainer;

public class Main {

    public static void main(String[] args) throws Exception {
        Server server = new Server(8112);
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        server.setHandler(context);
        ServletHolder h = new ServletHolder(new ServletContainer());
        h.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");
        h.setInitParameter("com.sun.jersey.config.property.packages", "resources");
        h.setInitOrder(1);
        context.addServlet(h, "/*");
        try
        {
            server.start();
            server.join();
        }
        catch (Throwable t)
        {
            t.printStackTrace(System.err);
        }

Trying to connect to this class: 试图连接到这个类:

package resources;


import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;


import java.util.ArrayList;
import java.util.List;

import pojo.Party;

@Path("/parties")
public class AllPartiesResource {

    @Context
    UriInfo url;

    @Context
    Request request;

    String name;

    public static final Timer allTime = DBConnection.registry.timer(MetricRegistry.name("Timer","all-parties"));

    @GET
    @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
    public List<Party> getAllParties() throws Exception
    {
        final Timer.Context context=allTime.time(); //start the timer 
        List<Party> list = new ArrayList<Party>();
        DBConnection.readAllData();
        list.addAll(DBConnection.getPartyCollection().values());
        context.stop(); //stops timer 
        return list;

//      ---> code for Jackson
//      String string; 
//      DBConnection.readAllData();
//      ObjectMapper jsonMapper = new ObjectMapper();
//      string=jsonMapper.writeValueAsString(DBConnection.getPartyCollection());
//      return string;
    }

    @GET
    @Path("count")
    @Produces(MediaType.TEXT_PLAIN)
    public String getPartyCount() throws Exception
    {
        DBConnection.readAllData();
        return String.valueOf(DBConnection.getPartyCollection().size());
    }

    @Path("{party}") //points to OnePartyResource.class
    public OnePartyResource getParty(@PathParam("party")String party)
    {
        name = party;
        return new OnePartyResource(url,request,party);
    }
}

You're mixing 2 versions of Jersey in your code together - ServletContainer from Jersey 2.x (package org.glassfish.jersey.* ) and properties from Jersey 1.x (package/prefix com.sun.jersey.* ). 您在代码中混合了两个版本的Jersey - 来自Jersey 2.x的ServletContainer (包org.glassfish.jersey.* )和来自Jersey 1.x的属性(包/前缀com.sun.jersey.* )。

To deploy your app using Jersey 2.x change these two lines 要使用Jersey 2.x部署您的应用,请更改这两行

h.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");
h.setInitParameter("com.sun.jersey.config.property.packages", "resources"); 

from your main method to 从你的main方法到

h.setInitParameter(ServerProperties.PROVIDER_PACKAGES, "resources");

and check the other ServerProperties you may find useful. 并检查您可能觉得有用的其他ServerProperties

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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