Domanda

Sto facendo un semplice "get" a Jboss / primavera.Voglio che il cliente mi passerà una serie di numeri interi nell'URL.Come posso impostare il server?E mostrare il cliente inviare il messaggio?

Questo è quello che ho adesso.

@RequestMapping(value="/test/{firstNameIds}", method=RequestMethod.GET)
@ResponseBody
public String test(@PathVariable List<Integer> firstNameIds)
{
     //What do I do??
     return "Dummy"; 
}
.

Sul cliente vorrei passare qualcosa come

http://localhost:8080/public/test/[1,3,4,50]
.

Quando ho fatto questo errore:

.

java.lang.illegalstateException: Impossibile trovare @Pathvariable [firstnameids] in @requestmapping

È stato utile?

Soluzione

GET http://localhost:8080/public/test/1,2,3,4

@RequestMapping(value="/test/{firstNameIds}", method=RequestMethod.GET)
@ResponseBody
public String test(@PathVariable String[] firstNameIds)
{
    // firstNameIds: [1,2,3,4]
    return "Dummy"; 
}
.

(testato con Spring MVC 4.0.1)

Altri suggerimenti

Dovresti fare qualcosa del genere:

Chiamata:

GET http://localhost:8080/public/test/1,2,3,4

Il tuo controller:

@RequestMapping(value="/test/{firstNameIds}", method=RequestMethod.GET)
@ResponseBody
public String test(@PathVariable List<Integer> firstNameIds) {
     //Example: pring your params
     for(Integer param : firstNameIds) {
        System.out.println("id: " + param);
     }
     return "Dummy";
}
.

Se si desidera utilizzare le parentesi quadre - []

DELETE http://localhost:8080/public/test/[1,2,3,4]

@RequestMapping(value="/test/[{firstNameIds}]", method=RequestMethod.DELETE)
@ResponseBody
public String test(@PathVariable String[] firstNameIds)
{
    // firstNameIds: [1,2,3,4]
    return "Dummy"; 
}
.

(testato con molla MVC 4.1.1)

Potrebbe fare ID di stringa @athvariable, quindi analizzare la stringa.

Quindi qualcosa come:

@RequestMapping(value="/test/{firstNameIds}", method=RequestMethod.GET)
@ResponseBody
public String test(@PathVariable String firstNameIds)
{
     String[] ids = firstNameIds.split(",");
     return "Dummy"; 
}
.

Avresti passato:

http://localhost:8080/public/test/1,3,4,50
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top