gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:54:05 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com