Java – how to configure embedded jetty to access Jersey resources?
•
Java
I'm trying to configure embedded jetty to talk to my jersey resources, but I can't figure out how to do this I've tried several different things, but it doesn't seem to work The jetty tutorial doesn't really deal with how to use Jersey Any code suggestions or links are highly 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); } }
Solution
You mixed two versions of Jersey in your code – from Jersey 2 X's servletcontainer (package org. GlassFish. Jersey. *) and from Jersey 1 Attributes of X (package / prefix com. Sun. Jersey. *)
To use Jersey 2 X to deploy your app, please 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");
From your main method to
h.setInitParameter(ServerProperties.PROVIDER_PACKAGES,"resources");
And view other server properties that you might find useful
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码