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

发布于 2024-11-05 19:18:42 字数 1738 浏览 3 评论 0原文

我有一个安静的应用程序,可以将一些联系人导入数据库。我想在未通过 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);
}

}

我的休息控制器

@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");
    }

I have a restful application that imports some contacts to database. I want to throw exception when a file is not uploaded via restful. so i write an exception class DenemeException, and i want to send status 404, when i run code i get this error org.jboss.resteasy.spi.UnhandledException: exception.DenemeException: off bu exler
can u help me pls?

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

}

My Rest 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");
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

衣神在巴黎 2024-11-12 19:18:42

您需要使用 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;
    }
}

You need to register the ExceptionMapper using the Application , eg

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文