如何配置嵌入式码头来访问泽西岛资源? [英] How to configure embedded jetty to access Jersey resources?

查看:138
本文介绍了如何配置嵌入式码头来访问泽西岛资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试配置嵌入式码头来与我的Jersey资源对话,但是我不知道该怎么做.我尝试了几种不同的方法,但似乎没有任何效果.跳船教程并没有真正处理如何使用Jersey.任何代码建议或链接都​​将不胜感激

I'm trying to configure embedded jetty to talk to my Jersey resources but I can't figure out how to do it. 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. Any code suggestions or links are greatly appreciated

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

推荐答案

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

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 2.x部署您的应用,请更改这两行

To deploy your app using Jersey 2.x change these two lines

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

从您的main方法到

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

并检查其他 ServerProperties ,您可能会觉得有用

and check the other ServerProperties you may find useful.

这篇关于如何配置嵌入式码头来访问泽西岛资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆