329

Handling un-mapped Rest path

This is my current Guice configuration:

public class MyServletModule extends ServletModule { @Override protected void configureServlets() { bind(MyRest.class); serveRegex(".+(?<!\\.(html|css|png|jpg))") .with(HttpServletDispatcher.class); } }

However I want that my Rest resource is only access in form of http://127.0.0.1:8888/{hashcode_or_filename} and the only form accepted and processed (well, plus the /create method below).

Right now, I can deal with hashcode and filename properly in this path pattern.

However I am not sure how to deal the kind or scenario below, where the client is requesting path that is not mapped, which returns this in my case:

Could not find resource for relative : /examples/foo of full path: http://127.0.0.1:8888/examples/foo

or

Could not find resource for relative : /examples/bar/foo of full path: http://127.0.0.1:8888/examples/bar/foo

What I need is to be able to be able to handle unmapped paths so I can return a error HTML page or something and not show these error text in the browser.

Also if the request is: http://127.0.0.1:8888/ I need to forward to http://127.0.0.1:8888/index.html automatically. As right now I have to manually put the index.html in the tail.

My Resteasy resource is configure or wired with just:

@Singleton @Path("/") public class MyRest { @GET @Path({hashcode}) public Response getSomething(...){} @POST @Path("create") public Response createSomething(...){} }

Answer1:

Easiest way is to register filter to handle responses with error code other that 200 (OK). Or add to your web.xml something like this:

<error-page> <error-code>404</error-code> <location>/ErrorPage.jsp</location> </error-page>

Also if the request is: http://127.0.0.1:8888/ I need to forward to http://127.0.0.1:8888/index.html automatically. As right now I have to manually put the index.html in the tail.

You can use this module http://tuckey.org/urlrewrite/

<strong>WEB-INF/web.xml</strong>

<filter> <filter-name>UrlRewriteFilter</filter-name> <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> <init-param> <param-name>confPath</param-name> <param-value>/WEB-INF/urlrewrite.xml</param-value> </init-param> <!--...omitted...--> </filter> <filter-mapping> <filter-name>UrlRewriteFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping>

<strong>WEB-INF/urlrewrite.xml</strong>

<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN" "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd"> <urlrewrite> <rule match-type="regex"> <from>^/$</from> <to type="redirect">/index.html</to> </rule> </urlrewrite>

Recommend

  • Java: equals and ==
  • using Natural order with treeset in java
  • How to use ES6 Hash Map on any object without maintaing a reference (I.e. Java hashcode)
  • Use @NodeEntity on interface/abstract class
  • Resolving DateTime Difference in PHP
  • android 2.2 WebView and WebViewClient RTSP problem
  • SecurityError for same-origin image texImage2D
  • How can I install and use latest Git on Mac OS X 10.9?
  • MySQL Query Tuning - Why is using a value from a variable so much slower than using a literal?
  • C# foreach - Is collection computed with each iteration? [duplicate]
  • Jackson mapper write id instead of entire object
  • why 0.1+0.2-0.3= 5.5511151231258E-17 in php [duplicate]
  • Spring security - same page to deliver different content based on user role
  • How to enable mapping the private property of the entity
  • What is corresponding c++ data type to SQL numeric(18,0) data type?
  • Do query loads all the data in memory
  • what makes a request a new request in asp.net C#
  • List images(01.png) and descriptions(01.txt) from directory
  • how to upload multiple files in c# windows application
  • Spring Cloud Microservice Architecture Confusion
  • Transactional Create with Validation in ServiceStack Redis Client
  • Allowing both email and username for authentication
  • How to install a .deb file on a jailbroken iphone programmatically?
  • Paperclip, set path outside of rails root folder
  • PHP - How to update data to MySQL when click a radio button
  • script to move all files from one location to another location
  • Fill an image in a square container while keeping aspect ratio
  • How to make Safari send if-modified-since header?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • How to pass list parameters for each object using Spring MVC?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Memory offsets in inline assembly
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • JaxB to read class hierarchy
  • Is there any way to bind data to data.frame by some index?
  • reshape alternating columns in less time and using less memory
  • Reading document lines to the user (python)
  • How can i traverse a binary tree from right to left in java?