[英]GET request for receiving the parameter from android in spring REST API?
我正在嘗試編寫spring REST代碼以獲取android用戶發送的參數。 例如,android用戶填寫表格,然后單擊發送按鈕。 現在,我想在REST API中接收值或參數。 我搜索谷歌,但不知道如何去做。 下面是我嘗試過的代碼,但是沒有用
EmailController.java
package com.intern.training
import java.awt.PageAttributes.MediaType;
import java.util.Map;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.WebRequest;
@RestController
@RequestMapping("/email")
public class EmailController
{
@RequestMapping(method= RequestMethod.GET)
public void getAll(WebRequest webRequest)
{
Map<String,String[]>params=webRequest.getParameterMap();
System.out.println(params);
}
}
我強烈建議您不要發送帶有GET
請求的參數。 首選POST
請求。 (請參閱跨站點偽造 )
然后,創建一個代表要接收的參數的類:
public class RequestParams {
private String name;
private String surname;
//Getters, Setters...
}
然后,將此對象作為您方法的參數:
@RequestMapping(method= RequestMethod.POST)
/**
Pay attention to the above @RequestBody annotation
or you will get null instead of the parameters
**/
public void getAll(@RequestBody RequestParams request)
{
request.getName();
request.getSurname();
//...
System.out.println(request.getName() + " " + request.getSurname());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.