【发布时间】:2018-10-20 12:35:39
【问题描述】:
@GET
@Path("/paises/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response findCuntryList(@PathParam("id")int id){
try{
ArrayList<Country> lista = new ArrayList<>();
for(int i=0; i<10 ;i++){
Country c = new Country();
c.setId(i);
c.setName("country"+i);
c.setCode("countryIsoCode"+i);
c.setRegion("region"+i);
lista.add(c);
}
Country co = lista.stream().filter(x -> x.getId()==id).findAny().get();
if(id > lista.size()-1) throw new Exception("That id is not correct");
return Response.ok(co).build();
}catch(Exception e){
return Response.status(404).entity(e.getMessage()).build();
}
}
当我没有异常时,我想返回一个 json,但是当我有它时,我需要返回一个带有异常消息的字符串,但是这个 json 解析的丢弃错误。
【问题讨论】:
-
你试过查询参数吗??
-
@RajanDesai 不,抱歉,我正在学习这项新技术。你能教我或放一些代码吗?谢谢
-
对我来说..解决这个问题的正确方法是在 catch 下使用 throw 并使用 @Provider创建 ExceptionHandler 类> 对于那个特殊的例外。
标签: java json maven jersey grizzly