2017-02-09 7 views
1

JAX-RS로 REST API를 개발 중입니다. 나는 this tutorial을 따라 갔고 이제는 앱을 잘 운영하고있다. 하지만 URL 경로에 문제가 있습니다. 사용자가 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 

: 그리즐리 자동으로 나는 이런 내 자신의 경로를 CH3OH 주요 방법에 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입니다.

그럼 어떻게 사용자에게 잘못된 URL을 요청했는지 알려주는 400 Bad Request을 어떻게 표시 할 수 있습니까? 그리고 Grizzly가 제공하는 기본 오류 메시지를 어떻게 변경합니까?

ExceptionMappers를 비롯한 사용자 정의 오류 메시지를 만들려고했지만 그게 내가 찾고있는 것이 아닌 것 같습니다. 내가 생각할 수있는 한 가지 해결책은 URL 경로에있는 각 /에 대한 새 클래스를 만드는 것이지만 매우 우아한 접근 방식은 아닙니다 ...? 다음은

는 내가 발견

@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

그때 내 API에 표시 리소스를 가져 오는 곳에서 다른 REST API를 연결 내 자원 클래스 나 자신에 대한 해결책. 모든 문자열과 일치하는 정규식을 사용하여 경로로 주석 처리 된 리소스를 사용할 수 있다는 것을 알았습니다. 나는 그것을 시험해 보았는데 그리즐리가 다른 '일하는'자원이 발견되지 않는 경우에만 그것에 맞는다는 것을 알게되었다.

덕분에 peeskillet's answerderabbink's question에 내가 그 아이디어를 발견했습니다. 사용자가 올바른 BASE URI가있는 경로를 입력하면 이제

@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 
    } 
} 

하지만 잘못된 리소스 경로 예 : 아래

내가 만든 자원 "http://localhost:8080/app/api/1.0"/invalid/path 내 주문에 넣은 내용이 모두 반환됩니다 Response.