2
votes

I am trying to implement a Rest Api,the code seems correct and simple but i am getting this error and I can't figure out the problem. enter image description here

The Log is outputting the following.

2017-10-10 14:49:40.946 WARN 5750 --- [nio-8080-exec-4] o.s.web.servlet.PageNotFound : Request method 'GET' not supported

     @RestController("/report")
     @CrossOrigin(origins = { "http://localhost:4200" })
     public class JasperController {

       @RequestMapping(value = "/allReports", method = { RequestMethod.GET }, produces = "application/json")
         public String allReport() {
                        return "allReports!!!";
                    }

       @RequestMapping(value = "/supportedFields", method = { RequestMethod.GET }, produces = "application/json")
         public List<String> supportedFields() {
                        return Arrays.asList("name", "age", "address", "code", "contract");
        }

   }
1
Your allowed origin is "localhost:4200", and from browser you're calling from "localhost:8080". Ports doesnt match, so your origin is not allowedLeffchik
I deleted it and the same error is still raising, i don't think the problem is the allowed origin.SEY_91
Weird.. R U sure that your controller package is actually being scanned by Spring?Leffchik
Yes i am sure, if I change one RequestMethod parameters from value = "/allReports" to name="/allReports" the error will disappear but if I enter /supportedFields in the browser it will call /allReports. And if I change the two RequestMethod parameters to name instead of value then another exception is raised There is already '/report' bean method public java.lang.String controller.JasperController.allReport() mapped.SEY_91

1 Answers

4
votes

It's pretty simple. The value of @RestController is not the mapping for it. It's the mistake I've made a lot. If you want a top-level mapping for all the methods in your controller, declare it with @RequestMapping on top of your controller class.

@RestController
@RequestMapping("/report")
public class JasperController {

Here is what the value on @RestController and @Controller is:

The value may indicate a suggestion for a logical component name, to be turned into a Spring bean in case of an autodetected component.