java - 异常映射 - org.jboss.resteasy.spi.UnhandledException : exception. DenemeException: off bu exler

标签 java jax-rs resteasy

我有一个 Restful 应用程序,可以将一些联系人导入数据库。当文件未通过 restful 上传时,我想抛出异常。所以我写了一个异常类 DenemeException,我想发送状态 404,当我运行代码时我得到这个错误 org.jboss.resteasy.spi.UnhandledException: exception.DenemeException: off bu exler 你能帮我吗?

DenemeExceptionMapper.java

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 DenemeExceptionMapper implements ExceptionMapper<DenemeException> {
     @Override
 public Response toResponse(DenemeException ex){
     return Response.status(Response.Status.FORBIDDEN).entity(ex.getMessage()).type(MediaType.TEXT_PLAIN).build();
 }

}

DenemeException.java

public class DenemeException extends Exception {
/**
 * 
 */
private static final long serialVersionUID = 1L;

public DenemeException(String message) {
    super(message);
}

我的休息 Controller

@POST
@Path("/import")
@Produces({"application/xml","application/json"})
public String saveContacts (@Context HttpServletRequest request, @Context HttpServletResponse response,
         @QueryParam("alt") String alt) throws DenemeException {

    byte[] content = null;

    FileItemStream item = null;
    try{
    ServletFileUpload upload = new ServletFileUpload();
    FileItemIterator iterator = upload.getItemIterator(request);

    while (iterator.hasNext()) {
        item = iterator.next();
        if ("fileUpload".equals(item.getFieldName())){

            content = IOUtils.toByteArray(item.openStream());

          }
        }
    }
    catch(Exception e){
        System.out.println("hata oldu");
        throw new DenemeException("off bu exler");
    }

最佳答案

您需要使用 Application 注册 ExceptionMapper ,例如

public class HelloWorldApplication extends Application {

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

    public HelloWorldApplication() {
        // ADD YOUR RESTFUL RESOURCES HERE
        this.singletons.add(new SimpleServiceImpl());
        this.singletons.add(new AuthenticationServiceImpl());
        this.singletons.add(new DenemeExceptionMapper());  //<<< -- HERE IS YOUR EXCEPTION MAPPER !
    }

    public Set<Class<?>> getClasses() {
        return this.empty;
    }

    public Set<Object> getSingletons() {
        return this.singletons;
    }
}

关于java - 异常映射 - org.jboss.resteasy.spi.UnhandledException : exception. DenemeException: off bu exler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5893685/

相关文章:

java - 设置数据库数组中 ListView 行的背景颜色

java - 在 netbeans 中单击“运行”时不显示 Jpanel

java - Swagger Codegen,Maven 插件 : Restrict Server Generation

java - JAX-RS 与 RESTeasy Factory

java - 有没有办法阻止用户访问 Resteasy 中的 PUT、POST 和 DELETE 资源?

java - 无法解释的 AmazonS3Client getObject() 403 AccessDenied 异常

java - OSGI 容器未加载 postgresql 驱动程序?

java - JAX-RS - Wink - 使用 Wink 客户端读取文件的正确方法

java - Jersey 和 Jackson 的 REST-Web 服务示例中出现 404 错误

file-upload - 如何防止 HTTP Rest Jboss resteasy Fileupload 因 'Could not find message body reader' 错误而失败?