JAX-RS Application on the root context - how can it be done?

前端 未结 5 1860
南旧
南旧 2020-12-04 16:39

I would like to have my JAX-RX Application start at the root context so my URLs will be

http://example.com/restfullPath

and not

http://example.com

5条回答
  •  情书的邮戳
    2020-12-04 17:14

    I have found another solution that involves internal Jersey classes, I assume it's probably just not yet part of the JAX-RS spec. (based on: http://www.lucubratory.eu/simple-jerseyrest-and-jsp-based-web-application/)

    web.xml

    
      jersey-rest-jsp-frame-1
    
      
        jersey
        
          com.sun.jersey.spi.container.servlet.ServletContainer
        
        
          
            com.sun.jersey.config.property.JSPTemplatesBasePath
          
          /WEB-INF/jsp
        
        
          
            com.sun.jersey.config.property.WebPageContentRegex
          
          
            (/(image|js|css)/?.*)|(/.*\.jsp)|(/WEB-INF/.*\.jsp)|
            (/WEB-INF/.*\.jspf)|(/.*\.html)|(/favicon\.ico)|
            (/robots\.txt)
          
        
      
      
        jersey
        /*
      
    
    

    WEB-INF/jsp/index.jsp

    <%@ page contentType="text/html; charset=UTF-8" language="java" %>
    
    
    
    

    Hello ${it.foo}!

    IndexModel.java

    package example;
    
    import com.sun.jersey.api.view.Viewable;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.Context;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.core.Response;
    import java.net.URI;
    import java.util.HashMap;
    
    @Path("/")
    @Produces(MediaType.TEXT_HTML)
    public class IndexModel {
    
        @GET
        public Response root() {
          return Response.seeOther(URI.create("/index")).build();
        }
    
        @GET
        @Path("index")
        public Viewable index(@Context HttpServletRequest request) {
          HashMap model = new HashMap();
          model.put("foo","World");
          return new Viewable("/index.jsp", model);
        }
    }
    

    This seems to work, but I wonder if it is / will be part of JAX-RS spec / implementation.

提交回复
热议问题