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

时间:2013-08-02 20:18:19

标签: java jersey jetty embedded-jetty

我正在尝试配置嵌入式jetty来与我的Jersey资源交谈,但我无法弄清楚如何做到这一点。我尝试过几种不同的东西但似乎没什么用。 jetty教程并没有真正处理如何使用Jersey。非常感谢任何代码建议或链接

编辑:

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

尝试连接此课程:

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

1 个答案:

答案 0 :(得分:10)

您在代码中将两个版本的Jersey混合在一起 - 来自Jersey 2.x的ServletContainer(包org.glassfish.jersey.*)和来自Jersey 1.x的属性(包/前缀{{1 }})。

要使用Jersey 2.x部署您的应用,请更改这两行

com.sun.jersey.*

从您的h.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig"); h.setInitParameter("com.sun.jersey.config.property.packages", "resources"); 方法

main

并查看您认为有用的其他ServerProperties