Giriş
Şu satırı dahil ederiz.
Şu satırı dahil ederiz.
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;- Eğer URL parametresine erişmek istersek @RequestParam anotasyonu kullanılır
Örnek
Elimizde şöyle bir URL olsun
/worklogs/worker/username?day=date Şöyle yaparız
@RestController@RequestMapping("/worklogs")public class WorklogController {    @GetMapping("/worker/{workerUserName}")  public List<Worklog> getReportForWorkerAndDay(    @PathVariable("workerUserName") String workerUserName,    @RequestParam("day") LocalDate day) {    return reportsService.getWorklogsForWorkerAndDay(workerUserName, day);  }}URL şöyle olsun
"localhost:8080/hello/someName"@ResponseBody
@RequestMapping(value = "/hello/{name}", method = RequestMethod.GET)
String hello(@PathVariable("name") String name) {
  return "hello " + name;
}Şöyle yaparız. @RequestMapping ile süslü parentez içinde bir değişken tanımlarız.
@RequestMapping(value = "/displaymessage/{msg}", method = RequestMethod.GET)
public String displayMessage(@PathVariable String msg, ModelMap model) {
  ...
}Bu anotasyon hep @RequestParam ile karıştırılıyor. @RequestParam kullanırken direkt URL içindeki parametrelere erişebiliriz. Şöyle yaparız.
@RequestMapping(value="/user/{userId}/invoices", method = RequestMethod.GET)
public List<Invoice> listUsersInvoices(
            @PathVariable("userId") int user,
            @RequestParam(value = "date", required = false) Date dateOrNull) {
  ...
}
 
Hiç yorum yok:
Yorum Gönder