1

I am trying to launch a Jetty server from a class. When I try to run it from eclipse it works fine. If I embed it to a JAR the jetty server starts, but when I make a request it return a 500 code response. Here is my class:

@GET
    @Path("test")
    @Produces(MediaType.APPLICATION_JSON)
    public String echo(@QueryParam("testParam")String test){
        return test;
    }

    private Server server; 

    public synchronized void start(int port) throws Exception {
        if (server != null) {
                throw new IllegalStateException("Server is already running");
            }

            ServletContextHandler context = new ServletContextHandler();
            context.setContextPath("/");
        Map<String,Object> initMap = new HashMap<String, Object>();
        initMap.put("com.sun.jersey.api.json.POJOMappingFeature", "true");
        initMap.put("com.sun.jersey.config.property.packages", "the.class.package");           
        context.addServlet(new ServletHolder(new ServletContainer(new PackagesResourceConfig(initMap))), "/*");

        this.server = new Server(port);
        this.server.setHandler(context);
        this.server.start();                   
    }

       public static void main(String[] args) throws Exception {
        if(args.length != 1) {
                System.out.println("AnnotatorServer <port>");
                System.exit(-1);
        }
        JettyServer server = new JettyServer();        
        server.start(Integer.parseInt(args[0]));
    }

When I try to launch it embedded in a JAR, the server starts, but when I access the method, I get the following exception:

    Caused by: com.sun.jersey.api.MessageException: A message body writer for Java   class java.lang.String, and Java type class java.lang.String, and MIME media     type application/octet-stream was not found

Do you have any idea why is this happening?? Thank you!

pokeRex110
  • 833
  • 2
  • 14
  • 26
  • can you elaborate on what you mean by 'embedded in a JAR', did you put the classes in the jar do did you put the jersey jar in the jar? – jesse mcconnell Oct 17 '12 at 16:02
  • I have a maven project and assembly the project using the maven-assembly-plugin, if you want I can add the maven pom file – pokeRex110 Oct 17 '12 at 16:17

1 Answers1

0

The problem was on the POM file. The dependency was overwriting some files in Meta-Inf/service. Found the solution to Jersey exception only thrown when depencencies assembled into a single jar

Community
  • 1
  • 1
pokeRex110
  • 833
  • 2
  • 14
  • 26