I'm working on a spring boot rest api and I want to take information from rest api to a html page. I tried this using jquery but I could not take data.

Here is my Rest Controller

package tr.kasim.Controller;

import java.sql.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tr.kasim.Service.PersonelService;
import tr.kasim.Service.PtsGirisCikisService;
import tr.kasim.Model.Personel;
import tr.kasim.Model.PtsGiriscikis;

public class STRestController {

public PersonelService personelService;

public PtsGirisCikisService ptsGirisCikisService;

public ResponseEntity<List<Personel>> getPersonels(){
    List<Personel> personels = personelService.findAll();
    return ResponseEntity.ok().body(personels);

public ResponseEntity<List<PtsGiriscikis>> getGirisCikis(){
    List<PtsGiriscikis> giriscikis = ptsGirisCikisService.findAll();
    return ResponseEntity.ok().body(giriscikis);

@RequestMapping(method=RequestMethod.GET, value="/api/personel", produces="application/json" )
public ResponseEntity<List<PtsGiriscikis>> getPersonelByKartNoAndÄ°seGiris(@RequestParam("kartno") String KartNo , @RequestParam("tarih") Date IseGirisTarihi){
    String errorMessage="Aranilan Personel Bulunamadi!";

    Personel personel = personelService.findPersonelByKartnoAndIsegiristarihi(KartNo , IseGirisTarihi);

    if(!personel.equals(null)) {
        List<PtsGiriscikis> girisCikis = ptsGirisCikisService.findByPersonelid(personel.getId());
        return ResponseEntity.ok().body(girisCikis);

        return null;


Here is my Controller

package tr.kasim.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

public class STController {

public String goHome() {
    return "templates/index";

Here is index.html page

<!DOCTYPE html>
    <title>Hello jQuery</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        function findPersonels() {
                url: "/api/personels",
                contentType: "application/json"
            }).then(function(data) {


        <p class="giriscikis-id">The ID is </p>
        <p class="giriscikis-ad">The name is </p>
        <input type="button" id="doldur" name="doldur" value="field doldur" onclick="findPersonels();">

I tried many jquery + spring application examples but I still got nothing. When I tried to reach index.html I just saw my html writings, rest service information are not showing. Please correct me, thank you.

1 Answers

cmoetzing On Best Solutions

With the limited information:

  1. I guess your mapping from path to template is wrong. Shouldn't it be like this?

    @GetMapping("/") public String goHome() { return "templates/index"; }

  2. An Uncaught ReferenceError indicates that your method is not found. Please verify the spelling (looks fine in your example, please check again in your code). In your example the braces of findPersonels are totally messed up. There is too many ). If that is corrected check in your browser console that there is no http404 when loading the page (so that js/personels.js is found).

  3. Your endpoint /api/personels defines a list as return type but your script uses the properties data.ad and data.id which do not look like they expect a list. The returned json probably looks something like this { items: [ { 'ad': 'first personel' 'id': '1' }, { 'ad': 'second personel' 'id': '2' } ] } The structure depends on your JSON marshaller. So a correct syntax for accessing your object in JS would be somehting like data.items[0].id.