[英]Java Spring Controller handling a ridiculous url
Okay so I am using a payment service called Thrive cart, I am doing this for a membership website I'm creating.好的,所以我正在使用名为 Thrive 购物车的付款服务,我正在为我正在创建的会员网站执行此操作。 When the user has paid I want them to be redirected to a URL where I can use that data to update the current users information.
当用户付款时,我希望他们被重定向到一个 URL,在那里我可以使用该数据来更新当前用户信息。
The data that get's sent in the params is insane:在 params 中发送的数据是疯狂的:
http://localhost:5000/user/welcome?thrivecart%5Baccount_id%5D=3196&thrivecart%5Baccount_name%5D=testacount&thrivecart%5Bcustomer%5D%5Bemail%5D=testname8%40gmail.com&thrivecart%5Bcustomer%5D%5Baddress%5D%5Bcountry%5D=GB&thrivecart%5Bcustomer%5D%5Baddress%5D%5Bzip%5D=pe303wu&thrivecart%5Border%5D%5B0%5D%5Bt%5D=product&thrivecart%5Border%5D%5B0%5D%5Bid%5D=6&thrivecart%5Border%5D%5B0%5D%5Bn%5D=Monthly+membership&thrivecart%5Border%5D%5B0%5D%5Bp%5D=799&thrivecart%5Border%5D%5B0%5D%5Bq%5D=1&thrivecart%5Border%5D%5B0%5D%5Bpo%5D=60120&thrivecart%5Border%5D%5B1%5D%5Bt%5D=product&thrivecart%5Border%5D%5B1%5D%5Bid%5D=6&thrivecart%5Border%5D%5B1%5D%5Bn%5D=Monthly+membership&thrivecart%5Border%5D%5B1%5D%5Bp%5D=799&thrivecart%5Border%5D%5B1%5D%5Bq%5D=1&thrivecart%5Border%5D%5B1%5D%5Bpo%5D=60120&thrivecart%5Border_currency%5D=GBP&thrivecart%5Border_id%5D=1041278&thrivecart%5Border_tax%5D=0.2&thrivecart%5Border_tax_id%5D=gb&thrivecart%5Border_total%5D=799&thrivecart%5Bpayment_processor%5D=paypal&thriv http://localhost:5000/user/welcome?thrivecart%5Baccount_id%5D=3196&thrivecart%5Baccount_name%5D=testacount&thrivecart%5Bcustomer%5D%5Bemail%5D=testname8%40gmail.com&thrivecart%5D%5Bcustomercart%5D%5Bcustomercart%5B5B 5D=GB&thrivecart%5Bcustomer%5D%5Baddress%5D%5Bzip%5D=pe303wu&thrivecart%5Border%5D%5B0%5D%5Bt%5D=product&thrivecart%5Border%5D%5B0%5D%5Bid&thrivecart%5Border%5D=D 5B0%5D%5Bn%5D=每月+会员&thrivecart%5Border%5D%5B0%5D%5Bp%5D=799&thrivecart%5Border%5D%5B0%5D%5Bq%5D=1&thrivecart%5Border%5D%5Border%5D%5B0 5D=60120&thrivecart%5Border%5D%5B1%5D%5Bt%5D=product&thrivecart%5Border%5D%5B1%5D%5Bid%5D=6&thrivecart%5Border%5D%5B1%5D%5Bnly%5D%5D%5Bnly%5D%cart%5D%5Bn 5D%5B1%5D%5Bp%5D=799&thrivecart%5Border%5D%5B1%5D%5Bq%5D=1&thrivecart%5Border%5D%5B1%5D%5Bpo%5D=60120&thrivecart%5Border_carid%tBq%5rD=Border_carid%5rD=5D 1041278&thrivecart%5Border_tax%5D=0.2&thrivecart%5Border_tax_id%5D=gb&thrivecart%5Border_total%5D=799&thrivecart%5Bpayment_processor%5D=paypal&thriv ecart%5Bproduct_id%5D=6&thrivecart%5Bpurchases%5D%5B0%5D=6&thrivecart%5Bpurchases%5D%5B1%5D=6&thrivecart_hash=a5b711d2288b4cb587511811bc0a3473
ecart%5Bproduct_id%5D=6&thrivecart%5Bpurchases%5D%5B0%5D=6&thrivecart%5Bpurchases%5D%5B1%5D=6&thrivecart_hash=a5b711d2288b4cb587511831473c
So far I've set up a simple controller which doesn't get hit:到目前为止,我已经设置了一个不会被击中的简单控制器:
@RestController
@RequestMapping("/user")
public class UserController {
@RequestMapping(value = "/welcome", method = RequestMethod.POST)
public void welcomeMember(@PathVariable String data) {
System.out.println(data);
}
}
How do I deal with crazy data like this?我如何处理这样的疯狂数据? Do I have to specific each path param?
我是否必须指定每个路径参数?
First of all, what you seem to get are not path elements but request parameters, so you will need @RequestParam annotations to get the values.首先,您似乎得到的不是路径元素而是请求参数,因此您需要@RequestParam 注释来获取值。
Since there are so many request parameters, I would also recommend to take just one parameter, a Map<String, String>
.由于请求参数太多,我还建议只使用一个参数,即
Map<String, String>
。 That Map will contain all the parameters as key/value pairs, for example:该 Map 将包含所有参数作为键/值对,例如:
If you're not sure whether you receive a POST or a GET request, you can also add a second parameter to receive the HttpMethod
.如果您不确定收到的是 POST 请求还是 GET 请求,您还可以添加第二个参数来接收
HttpMethod
。
Change your RestController to:将您的 RestController 更改为:
@RestController
@RequestMapping("/user")
public class UserController {
@RequestMapping(value = "/welcome")
public void welcomeMember(@RequestParam Map<String, String> data, HttpMethod method) {
System.out.println(method);
System.out.println(data);
}
}
That looks like a problem with how the rest api is called from the service consumer side.这看起来像是如何从服务消费者端调用 rest api 的问题。
try sending the data in a request body rather then as a param.尝试在请求正文中发送数据,而不是作为参数发送。 This way you can use a POJO to handle the data.
这样您就可以使用 POJO 来处理数据。
Question 1:问题 1: So far I've set up a simple controller which doesn't get hit:
到目前为止,我已经设置了一个不会被击中的简单控制器:
As per your URL http://localhost:5000/user/welcome "user" seems to be your projects context name.根据您的 URL http://localhost:5000/user/welcome "user" 似乎是您的项目上下文名称。 Try removing
@RequestMapping("/user")
from your class.尝试从您的班级中删除
@RequestMapping("/user")
。
Also, instead of @PathVariable String data
use @RequestParam Map<String,String> params
.此外,使用
@RequestParam Map<String,String> params
代替@PathVariable String data
。 @PathVariable String data
is used when data is part of url but in your case it's parameter.当数据是 url 的一部分时使用
@PathVariable String data
,但在您的情况下它是参数。 Final code should be like below.最终代码应如下所示。
@RestController
public class UserController {
@RequestMapping(value = "/welcome", method = RequestMethod.POST)
public void welcomeMember(@RequestParam Map<String,String> params ) {
for(Map.Entry<String, String> entry : params.entrySet()){
//This will print all paramters name and their value
System.out.println(entry.getKey() + "-" + entry.getValue());
}
}
}
Question 2: How do I deal with crazy data like this?问题2:我如何处理这样的疯狂数据? Do I have to specific each path param?
我是否必须指定每个路径参数?
I will suggest to follow standard practice.我会建议遵循标准做法。 Send data in json format.
以json格式发送数据。 There are different ways for this depend upon front end technology you are using.
这取决于您使用的前端技术,有不同的方法。 One way is Link
一种方法是链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.