【问题标题】:Grizzly JAX-RS NOT returning 400 Bad RequestGrizzly JAX-RS 未返回 400 错误请求
【发布时间】: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


    【解决方案1】:

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

    感谢peeskillet's answer 上的derabbink'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 中放入的任何内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-30
      • 2019-07-28
      • 2019-07-11
      • 2015-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多