newdev
newdev

Reputation: 59

AngularJS $http path 404 not found in Spring Boot App

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;

});

Upvotes: 1

Views: 377

Answers (3)

Nikhil Zurunge
Nikhil Zurunge

Reputation: 766

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

Upvotes: 2

Hema
Hema

Reputation: 986

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.

Upvotes: 1

Jayesh
Jayesh

Reputation: 999

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.

Upvotes: 1

Related Questions