1

I have the following String that I receive in my rest service:

.6.75:5050/pretups/C2SReceiver?REQUEST_GATEWAY_CODE=8050122997&REQUEST_GATEWAY_TYPE=EXTGW&LOGIN=CO8050122997_EXTGW&PASSWORD=89b87741ca3f73b0b282ae165bad7501&SOURCE_TYPE=XML&SERVICE_PORT=190

I have the following code:

@Controller
public class servicioscontroller {

 @RequestMapping(value = "/pretups/{p1}?{trama}", method = RequestMethod.GET)
 @ResponseBody
public String enviarTrama(@PathVariable("p1") String p1,@PathVariable("trama") String trama){

 return p1+trama;


 }
}

And im getting this result:

C2SReceive

I need also the string after that ?, what im doing wrong or how do I get that? thanks

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
BugsForBreakfast
  • 712
  • 10
  • 30
  • should be @RequestParam("trama") – Tao Dong Aug 27 '19 at 20:36
  • @Tao Dong I will try that – BugsForBreakfast Aug 27 '19 at 20:37
  • @TaoDong Can I use pathvariable and requestparam at the same time? and also whats the difference mate? – BugsForBreakfast Aug 27 '19 at 20:39
  • 1
    Your requestmapping don't seems right tho.. It might not contain ? You don't need to specify requestparam in requestmapping, they should be access by requestparam name in this case i.e. REQUEST_GATEWAY_CODE etc. – pks Aug 27 '19 at 21:05
  • 1
    You can use both pathvariable and requestparam the same time. In your case you can try String enviarTrama(\@PathVaraible("p1") String p1, \@PathVariable Map trama) which should give you all params as a map. A better solution is to implement as @pks suggests – Tao Dong Aug 27 '19 at 21:13
  • Yeah it worked by doing like pks suggest, if anyone wanna post an answer with more detailed stuff go ahead – BugsForBreakfast Aug 27 '19 at 21:26

1 Answers1

0

You should use @RequestParam:

@Controller
public class servicioscontroller {
    @RequestMapping(value = "/pretups/{p1}", method = RequestMethod.GET, params = ["trama"])
    @ResponseBody
    public String enviarTrama(
        @PathVariable("p1") String p1,
        @RequestParam(name = "trama", required = "true") String trama
    ){
        return p1+trama;
    }
}

Note that you have to put request params in the @RequestMapping's params attribute instead of the path string.

The difference between @RequestParam and @PathParam is described in this answer.

madhead
  • 31,729
  • 16
  • 153
  • 201