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>