2017-02-09 47 views
1

我正在使用JAX-RS開發REST API。我跟着this tutorial,現在我正在運行應用程序。但是我的網址路徑有問題。如果用戶輸入的BASE_URI錯例如Grizzly JAX-RS不返回400錯誤請求

// Base URI the Grizzly HTTP server will listen on 
public static final String BASE_URI = "http://localhost:8080/app/api/1.0 

:灰熊的主要方法,在這裏我addded我自己這樣的路徑自動創建BASE_URI"http://localhost:8080/ap/ap/1.0/path/to/myResourse/123"灰熊返回

Not Found 
Resource identified by path '/app/api/1.0/whatever/the/user/entered, does not exist. 
Grizzly 2.3.28. 

問題是,如果用戶輸入BASE_URI正確的,但進入我的資源路徑錯了,灰熊不顯示「未找到資源」的消息,但只顯示黑屏與HTTP頭404。

那麼如何顯示一個400 Bad Request,告訴用戶他向一個無效的URL請求?我如何更改Grizzly提供的默認錯誤消息?

我試過尋找創建自定義錯誤消息,包括ExceptionMappers,但我認爲這不是我正在尋找的。我能想到的一個解決方案是爲URL Path中的每個/創建一個新類,但這不是一個非常優雅的方法......?

下面是從那裏獲取資源,然後我在我的API顯示

@Path("/path/to/myResourse") 
public class ResourceService { 

    @GET 
    @Path("{id}") 
    @Produces(MediaType.APPLICATION_JSON + "; charset=UTF-8") 
    public Response getBuildingSite(@PathParam("id") String id) throws Exception { 

    StringBuilder sb = new StringBuilder(); 
    sb.append("https://www.exmaple.com/rest/api/resources"); 
    sb.append(id); 
    sb.append(".xml"); 
    String url = sb.toString(); 

    try { 
     Resource resource = Connector.fetch(Resource.class, url); 
     return JSONMapper.asOkJsonResponse(resource); 
    } catch (Exception e) { 
     return JSONMapper.asErrorJsonResponse(
     new ErrorResponse(404,"Resource '" + id + "' not found")); 
    } 
    } 
} 

我pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 

    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.example.app.exampleApp</groupId> 
    <artifactId>exampleApp</artifactId> 
    <packaging>jar</packaging> 
    <version>1.0-SNAPSHOT</version> 
    <name>exampleApp</name> 

    <dependencyManagement> 
    <dependencies> 
     <dependency> 
     <groupId>org.glassfish.jersey</groupId> 
     <artifactId>jersey-bom</artifactId> 
     <version>${jersey.version}</version> 
     <type>pom</type> 
     <scope>import</scope> 
     </dependency> 
    </dependencies> 
    </dependencyManagement> 

    <dependencies> 
    <dependency> 
     <groupId>org.glassfish.jersey.containers</groupId> 
     <artifactId>jersey-container-grizzly2-http</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.glassfish.jersey.media</groupId> 
     <artifactId>jersey-media-json-jackson</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.9</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.mariadb.jdbc</groupId> 
     <artifactId>mariadb-java-client</artifactId> 
     <version>1.5.7</version> 
    </dependency> 
    </dependencies> 

    <build> 
    <plugins> 
     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <version>2.5.1</version> 
     <inherited>true</inherited> 
     <configuration> 
      <source>1.8</source> 
      <target>1.8</target> 
     </configuration> 
     </plugin> 
     <plugin> 
     <groupId>org.codehaus.mojo</groupId> 
     <artifactId>exec-maven-plugin</artifactId> 
     <version>1.2.1</version> 
     <executions> 
      <execution> 
      <goals> 
       <goal>java</goal> 
      </goals> 
      </execution> 
     </executions> 
     <configuration> 
      <mainClass>com.example.app.exampleApp.Main</mainClass> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 

    <properties> 
    <jersey.version>2.25</jersey.version> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 
</project> 

回答

0

我發現了一個連接到另一個REST API我的資源類自己解決我的問題。我發現我可以使用正則表達式匹配所有字符串的路徑註釋的資源。我測試了它,發現只有在沒有找到其他「工作」資源的情況下,灰熊纔會匹配它。

感謝peeskillet's answerderabbink's question我發現了這個想法。

下面是我創建的資源:

@Path("{any: .*}") 
public class NotFoundService { 

    @GET 
    @Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8") 
    public Response getNotFound(@Context UriInfo uriInfo) { 
    // My custom response where I can use uriInfo to tell what went wrong 
    } 
} 

所以,現在如果用戶輸入具有正確的基URI路徑,但無效的資源路徑例如"http://localhost:8080/app/api/1.0"/invalid/path它會返回我放入我的自定義Response