2012-04-14 19 views
12

sto costruendo app REST web utilizzando Netbean 7.1.1 Glassfish 3.1.2REST come passare il parametro path vuoto?

Ho 2 URL:

"http://myPage/resource/getall/name" (get some data by name) 

"http://myPage/resource/getall" (get all data) 

Quando client invia richiesta utilizzando primo URL, il servlet di seguito si chiama e fare qualche processo.

@Path("getall/{name}") 
@GET 
@Produces("application/json") 
public Object Getall(@PathParam("name") String customerName) { 
     //here I want to call SQL if customerName is not null. is it possible??? 
} 

Ma voglio anche che il secondo URL chiami questo servlet.

Ho pensato che il servlet sarebbe stato chiamato e posso solo verificare customerName == null e quindi chiamare SQL diversi e così via.

Ma quando il client invia la richiesta utilizzando il secondo URL (cioè senza parametro path), il servlet non viene chiamato perché l'URL non ha il parametro percorso {name}.

Non è possibile chiamare il secondo URL e richiamare il servlet di cui sopra?

Un'alternativa mi viene in mente è quello di utilizzare query parameter:

http://myPage/resource/getall?name=value 

Forse posso analizzarlo e vedere se "value" è null poi agire di conseguenza ..

risposta

26

È possibile specificare un'espressione regolare per il tuo parametro Path (vedi 2.1.1. @Path).

Se si utilizza .* partite Entrambi i nomi vuoti vuoti e non Quindi, se si scrive:

@GET 
@Path("getall/{name: .*}") 
@Produces("application/json") 
public Object Getall(@PathParam("name") String customerName) { 
     //here I want to call SQL if customerName is not null. is it possible??? 
} 

essa corrisponderà sia "http: // myPage/risorsa/getall" e "http: // miaPagina/risorsa/getall/nome".

+0

Grazie, ho provato sopra e ha funzionato perfettamente! –

+0

Ciao! Che succede se la situazione è successiva: "http: // myPage/resource/getall/name? Type = json" "http: // myPage/resource/getall? Type = json" La soluzione sopra non funziona. – Andrew

+0

@Andrew si prega di creare una nuova domanda. Senza alcun contesto non è possibile rispondere alla domanda. – andih

-2
@GET 
@Path("getall{name:(/[^/]+?)?}") 
@Produces("application/json") 
public Object Getall(@PathParam("name") String customerName) { 
    //here I want to call SQL if customerName is not null. is it  

possible??? 
    } 
+0

corrisponde sia con che senza nome, –

Problemi correlati