0

My data is successfully inserted into the database but I still keep getting this error. It's performing okay but I'm irritated cause I keep getting this error.
This is my code:

$.ajax
({
type: 'post',
url: '/SendMessage',
data: 
{
    message_id: 0,
    message_sender: user_id,
    message_to: item,
    message_info: message,
    message_time: dateFormat,
    message_seen: "NO"

},
success: function (response) 
{
    alert("success");
},
});

This is my controller: private static final String PATH = "/error";

@RequestMapping(value="/SendMessage" , method=RequestMethod.POST)
public void sendMessage(Message message) {
    messageService.saveOrUpdate(message);
}

@RequestMapping(value= PATH) 
public ModelAndView error404() {
    ModelAndView model = new ModelAndView("error/error404");
    return model;
}

@Override
public String getErrorPath() {
    return PATH;
}

application.properties

spring.mvc.view.prefix= /WEB-INF/jsp/
spring.mvc.view.suffix= .jsp

server.error.whitelabel.enabled= false 
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration

Error:

Ambiguous handler methods mapped for HTTP path 'http://localhost:8080/error': {public org.springframework.web.servlet.ModelAndView com.rtc_insurance.controller.SubmitController.error404(), public org.springframework.web.servlet.ModelAndView com.rtc_insurance.controller.WebsiteController.error404()} at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:371) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]

Hoping you could help me. Thank you so much!!!

trumanblack1025
  • 471
  • 2
  • 8
  • 19

2 Answers2

0

From the error it looks like you have declared multiple methods with the same url 'error' or same method signature.

So spring is not able to decide which url to map because it is getting multiple methods on same url.

Alien
  • 15,141
  • 6
  • 37
  • 57
  • I don't know why is it redirecting to /error page. Could you please help me? – trumanblack1025 Nov 12 '18 at 04:41
  • add com.rtc_insurance.controller.WebsiteController.error404() code once. – Alien Nov 12 '18 at 04:47
  • I'm getting org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'basicErrorController' method – trumanblack1025 Nov 12 '18 at 05:35
  • https://stackoverflow.com/questions/25356781/spring-boot-remove-whitelabel-error-page/31006380 see this – Alien Nov 12 '18 at 05:36
  • I already tried following the link you gave. I've updated my code above. It's still giving me an error – trumanblack1025 Nov 12 '18 at 06:35
0

Assuming that the question is actually about the title of the question (Ambiguous handler methods mapped for HTTP path 'http://localhost:8080/error' for POST)

I can say that:

  • Your controller and Ajax request are ok and are really irrelevant here
  • There is a mapping on /error that appears in one of your controllers

Spring has its own error handling (that can be overridden but probably not like you've tried to do) and it also maps to /error, so there is a clash of mappings here.

You can read more about spring boot error handling here (especially this part can be relevant)

Update: After you've updated the question, please take a look at com.rtc_insurance.controller.WebsiteController.error404 It must be your controller that is not compliant with spring boot error handling mechanisms mentioned in a links above in my answer

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • It's appearing on every ajax post/get that I submit. :( – trumanblack1025 Nov 12 '18 at 04:45
  • Check mapping of `com.rtc_insurance.controller.WebsiteController.error404` and read the documentation that I've sent :) You'll see the reason – Mark Bramnik Nov 12 '18 at 04:46
  • Hey I've read the documentation and tried to update my code. Right now I'm getting a different error. I've updated my code above – trumanblack1025 Nov 12 '18 at 06:22
  • so try to remove error404() method from the controller added in the question... the error should disappear. Since you read the documentation you know how to map it correctly now... – Mark Bramnik Nov 12 '18 at 06:42