简体   繁体   English

如何在 RESTful Web 服务中正确使用 Java 上的 @Path 表示法

[英]How to properly use @Path notation on java in RESTful Web services

@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Path("{idinst: [0-9]+ }/, { tipopersona}/")
public List<String> obtenCorreosDeInstitucion(@PathParam("idinst") long idinst,@PathParam("tipopersona") String tipo){
    List<String> resultado=new ArrayList<>();
    List<Persona> persona=obtenerPersona();
    int length=persona.size();
    int i=0;
    while(i<length){
        if(persona.get(i).getTipoPersona().equals(tipo)){
            if(persona.get(i).getIdInstitucionPersona()==idinst){
                resultado.add(persona.get(i).getEmailPersona());
            }
        }
        i++;
    }
    return resultado;
}

public List<Persona> obtenerPersona(){
    List<Persona> resultado=new ArrayList<>();
    try{
        Query q = em.createQuery("SELECT p FROM Persona p");
        Persona p = (Persona) q.getResultList();
        resultado= (List<Persona>) q.getResultList();
        System.out.println(resultado);

    }
    catch(NoResultException ex){

    }
    return resultado;
}

So I have the code like this, but on the tests, it says 404 not found, but the other methods in this class do pass the tests, so I am assuming something in my path is not right, can someone please point me in the right direction?所以我有这样的代码,但是在测试中,它说 404 not found,但是这个类中的其他方法确实通过了测试,所以我假设我路径中的某些东西是不正确的,有人可以指点我吗正确的方向? I have searched up on the internet and I can only find examples for when it's only one variable value you get from the path.我在互联网上搜索过,我只能找到当它只是从路径中获得的一个变量值时的示例。

Try the following (it seems to me that you have extra , in your @Path annotation):尝试以下操作(在我看来,您的@Path注释中有额外的, ):

@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Path("{idinst: [0-9]+ }/{tipopersona}/")
public List<String> obtenCorreosDeInstitucion(@PathParam("idinst") long idinst,@PathParam("tipopersona") String tipo){
    List<String> resultado=new ArrayList<>();
    List<Persona> persona=obtenerPersona();
    int length=persona.size();
    int i=0;
    while(i<length){
        if(persona.get(i).getTipoPersona().equals(tipo)){
            if(persona.get(i).getIdInstitucionPersona()==idinst){
                resultado.add(persona.get(i).getEmailPersona());
            }
        }
        i++;
    }
    return resultado;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM