【发布时间】:2017-02-09 06:30:00
【问题描述】:
我正在使用 JAX-RS 开发一个 REST API。我关注了 this tutorial ,现在我正在运行该应用程序。但我对 URL 路径有疑问。 Grizzly 在 main 方法中自动创建了BASE_URI,我在其中添加了自己的路径,如下所示:
// Base URI the Grizzly HTTP server will listen on
public static final String BASE_URI = "http://localhost:8080/app/api/1.0
如果用户输入错误的 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正确,但输入我的资源路径错误,Grizzly不会显示“找不到资源”消息,而只是显示一个空白屏幕HTTP 标头为 404。
那么我怎样才能显示一个400 Bad Request,告诉用户他向一个无效的 URL 发出了请求?以及如何更改 Grizzly 提供的默认错误消息?
我尝试搜索创建自定义错误消息,包括 ExceptionMappers,但我认为这不是我要搜索的内容。我能想到的一个解决方案是为 URL 路径中的每个 / 创建一个新类,但这不是一个非常优雅的方法......?
下面是我的 Resource 类,它连接到另一个 REST API,从中获取资源,然后我将其显示在我的 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>
【问题讨论】:
标签: java rest maven jax-rs grizzly