我如何使用 REST easy 捕获错误的 URL 请求

How do i catch bad URL requests using REST easy

我在“/rest/role/"whatever"

下有几个端点

我如何捕获错误的请求,例如“rest/role/dbbhwbhb/wdwdwed

如果我使用:

    @GET
    @Path("/{param}")
    @Produces(MediaType.TEXT_PLAIN)
    public Response badURLS() {
        return Response.ok().entity("bad url").build();
    }

这抓住了 rest/roles/dfsdfsds 但 rest/roles/sdsasd/asdad 仍然没有被抓住。如何设置一种方法,以便如果 none 我的端点被命中然后 return 默认消息?

JBoss接近

RESTeasy 方法

编辑

正如我之前发送给您的那样 - 第二个 link 就可以了。

鉴于这样写的映射器和结构:

package atata;

import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class _404Handler implements ExceptionMapper<NotFoundException> {

    public Response toResponse(NotFoundException exception) {
        return Response.status(500).entity(new ResponseEntity(333, "Got 404.")).type(MediaType.APPLICATION_JSON).build();
    }

}

配置

package atata;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.core.Application;


public class EmployeeApplication extends Application {

    private Set<Object> singletons = new HashSet<Object>();

    public EmployeeApplication() {
        singletons.add(new MessageRestService());
        singletons.add(new _404Handler());
    }

    @Override
    public Set<Object> getSingletons() {
        return singletons;
    }

}

Pom:

<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>io.atata</groupId>
    <artifactId>atata</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>
    <name>atata Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jaxrs -->
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>3.0.19.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-servlet-initializer</artifactId>
            <version>3.0.4.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jackson-provider</artifactId>
            <version>3.0.14.Final</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>atata</finalName>
    </build>

端点:

数据包;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/roles")
public class MessageRestService {

    @GET
    @Path("/1")
    @Produces(MediaType.APPLICATION_JSON)
    public Response badURLS() {
        return Response.ok().entity("OK").build();
    }

}

您将获得