1

I'm having trouble finding the right path to use in the $http requests in order to access the @RestController in my Spring Boot App. I'll include the relevant code below, hoping that you'll see something i'm not. I apologize that code is not fully in english, but i think it will be easy to understand.

Here's the backend rest controller that i'm suppoused to target:

@RestController
@RequestMapping("/fileData")
public class FileDataService {

    @Autowired
    HttpServletRequest request;
    @Autowired
    ServletContext ctx;

    @RequestMapping(method = RequestMethod.GET,
            value = "fileData/getKorisnici", 
            produces = MediaType.APPLICATION_JSON_VALUE)
    //@ResponseBody
    public Collection<Korisnik> getKorisnici() {
        return getFileData().getKorisnikValues();
    }

    /*@POST
    @Path("/addKorisnik")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)*/
    @SuppressWarnings("rawtypes")
    @RequestMapping(method = RequestMethod.POST,
            value = "/addKorisnik", 
            produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    //@ResponseBody
    public ResponseEntity addKorisnik(Korisnik k) {
        if(getFileData().usernameExistsKorisnici(k.getKorisnickoIme())) {
            return ResponseEntity.status(HttpStatus.CONFLICT).build();
        }
        else if(getFileData().emailExistsKorisnici(k.getEmail())) {
            return ResponseEntity.status(HttpStatus.CONFLICT).build();
        }
        getFileData().getKorisnici().put(k.getIdKorisnik(), k);
        getFileData().writeData();
        return ResponseEntity.ok().build();
    }

And here's the frontend side:

app.factory('korisnikFactory', function($http) {

    var factory = {};
    factory.getKorisnici = function() {
        //return $http.get('/drools-spring-v02-app/rest/fileData/getKorisnici');
        return $http.get('/fileData/getKorisnici');
    };

    factory.addKorisnik = function(korisnik) {
        return $http.post('/fileData/addKorisnik', korisnik);
    };

    return factory;

});

3 Answers 3

2

In your springboot app there's an annotation @RequestMapping("/fileData") defined above the class FileDataService, Hence every request call is prefixed by /fileData/.. automatically. There's no need to write it again as in your get call

@RequestMapping(method = RequestMethod.GET, value = "fileData/getKorisnici", produces = MediaType.APPLICATION_JSON_VALUE)

while in AngularJS there should be absolute path return $http.get($location.absUrl() + 'fileData/getKorisnici'); (mind those slashes)

You can refer one of my Demo project on SpringBoot+AngularJS here

Sign up to request clarification or add additional context in comments.

Comments

1

I believe your relative url may be wrong here. First, try to get the full path of your spring boot app and try to use RestClient or Postman to see if the spring boot app is working fine for both GET and POST request. After this step, come back to your Angular try $http.get([your-full-url') if that works out then you will be able to update the relative url.

Comments

1

You have made a slight mistake with URL formation, In your class, u have declared @RequestMapping("/fileData") so your methods should follow path preceding this. You should not again include value = "fileData/getKorisnici".

Just try correcting it to value = "/getKorisnici" everything will work fine.

Nothing to be changed in your front end side.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.