gpt4 book ai didi

java - Grizzly JAX-RS 未返回 400 错误请求

转载 作者:太空宇宙 更新时间:2023-11-04 11:48:05 52 4
gpt4 key购买 nike

我正在使用 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 不会显示“找不到资源”消息,而只是显示带有 404 HTTP header 的空白屏幕。

那么如何显示 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>

最佳答案

我自己找到了解决问题的方法。我发现我可以使用由路径注释的资源以及匹配所有字符串的正则表达式。我测试了它,发现只有在没有找到其他“工作”资源时,grizzly 才会与它匹配。

感谢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 中放入的任何内容。

关于java - Grizzly JAX-RS 未返回 400 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42129736/

52 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com