2017-02-09 9 views
1

JAX-RSでREST APIを開発しています。私はthis tutorialをフォローしています。今はアプリを正常に動かしています。しかし、私はURLパスに問題があります。ユーザーがBASE_URIを間違って入力した場合、例えばGrizzly JAX-RSが400 Bad Requestを返さない

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

:グリズリーは自動的に私はこのような自分の道をaddded mainメソッドに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を含むカスタムエラーメッセージの作成を試みましたが、私が探しているものではないと思います。私が考えることができる1つの解決策は、URLパスに各/の新しいクラスを作成することですが、それは非常にエレガントなアプローチではありません...?

以下

が、私はその後、私の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 
    } 
} 

だから今、ユーザーが正しいBASE URIが、無効なリソースパスを持っているパスを入力した場合、例えば"http://localhost:8080/app/api/1.0"/invalid/path私は自分のカスタムに入れたものを返しますResponse

関連する問題