0

I have a JSP page that makes an ajax post to a Java spring controller. However, whenever I make the post, I am getting a 415 Unsupported Media Type error. Since I'm fairly new to Spring MVC, I'm not sure if the error is due to my response type or request type. My ajax looks like:

$.ajax({
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            type: "POST",
            url: "validateRedirect",
            context:document.body,
            contentType:"application/json",
            data:JSON.stringify(validateObject),
            dataType:"json"
        });

With my request mapping looking like:

@RequestMapping(value = "/validateRedirect", method = {RequestMethod.POST}, headers="Content-Type=application/json")
public ResponseEntity<String> callToValidate(HttpServletRequest servletRequest, HttpServletResponse servletResponse, @RequestBody ValidateObj validateObject) 

Even when I try to make a post from Postman, I get the same error, so I'm thinking it's something to do with my response

2
  • And just a note, I have <mvc:annotation-driven/> in my servlet.xml Commented Jan 29, 2019 at 3:53
  • check this -> stackoverflow.com/questions/11492325/… Commented Jan 29, 2019 at 3:57

2 Answers 2

1

Try modifying your controller to

@PostMapping(value = "/validateRedirect")
public ResponseEntity<ValidateObj> callToValidate(@RequestBody ValidateObj validateObject)
Sign up to request clarification or add additional context in comments.

Comments

0

try it out:

@RestController
public TestController{
    @PostMapping("/validateRedirect")
    public ResponseEntity<ValidateObj> callToValidate(@RequestBody ValidateObj validateObject){
        //...your logic
        return new ResponseEntity<ValidateObj>();
    }
}

If you are using default settings of spring, it should application/json as default accept and content type. So you don't have to specify in annotation.

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.