如何使用REST轻松捕获错误的URL请求

时间:2016-10-10 17:05:50

标签: java jboss jax-rs resteasy

我在“/ rest / role /”下有几个端点“

如何捕获不良请求,例如“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。如何设置方法,以便在没有命中任何端点时返回默认消息?

1 个答案:

答案 0 :(得分:0)

JBoss方法

RESTeasy方法

修改

正如我之前发给你的那样 - 第二个链接可以解决问题。

鉴于以这种方式编写的映射器和结构:

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;
    }

}

波姆:

<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>

端点:

包atata;

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();
    }

}

你会得到

enter image description here

enter image description here