0

enter image description hereI want to pass a boolean value from my controller to javascript using json but couldnot find a way as I am new to spring mvc.

While using servlet we wrote:

response.getWriter().println(somevalue)

and the somevalue can be received using ajax. Here my controller method is:

@RequestMapping(value = REGISTERACTION , method = RequestMethod.POST)
@ResponseBody
public boolean RegisterUser(@ModelAttribute("register") Register register,HttpServletRequest request, HttpServletResponse response)
{

    boolean Registrationsuccess = userService.RegisterUser(register);
    return Registrationsuccess;
}

So, here the boolean variable is Registrationsuccess which I want to send to js file and receive it using ajax.

And in my javascipt function which is called using onsubmit event-->

 function AccountExists()
 {
 $.ajax({
        type: 'POST',
        url: 'registerProcess',
        success: function(data){    
        let detail = JSON.parse(data);
            if( data == true)
                alert("Success");
            else
                alert("Not ");
        }
 });

}

Getting error --

The target resource does not have a current representation that would be acceptable to the user agent, according to the proactive negotiation header fields received in the request, and the server is unwilling to supply a default representation.

3 Answers3

0

Try to use @ResponseBody annotation in your controller's method. And change the return type of the method to Boolean, then return Registrationsuccess instead of ModelAndView.

Hưng Chu
  • 1,027
  • 5
  • 11
0

You need to use ResponseEntity and @RestController for JSON Response.

Note : @RestController includes both annotations @Controller and @ResponseBody.

Try with this :

@RestController
@RequestMapping("controller")
public class Controller {

    @PostMapping("REGISTERACTION")
    public ResponseEntity<Boolean> RegisterUser(@ModelAttribute("register") Register register)
    {

       Boolean registrationSuccess = userService.RegisterUser(register);
       return new ResponseEntity<Boolean>(registrationSuccess , HttpStatus.OK);
    }

}
Anish B.
  • 9,111
  • 3
  • 21
  • 41
  • 1
    what is the difference between @ResponseBody annotation and ResponseEntity?@Anish – Vidhi Agarwal Oct 18 '19 at 07:20
  • @VidhiAgarwal `@RestController` includes both annotations `@Controller` and `@ResponseBody` – Anish B. Oct 18 '19 at 07:25
  • @VidhiAgarwal `@ResponseBody` does is it converts the objects to suitable web response like JSON or XML. You can set it. The ResponseEntity is the full HTTP Response . You can set various things in that like status code, content-type and many more.. Please check docs for more info. – Anish B. Oct 18 '19 at 07:33
  • 1
    I have edited my question..Can you please Check @Anish – Vidhi Agarwal Oct 20 '19 at 08:25
  • @VidhiAgarwal see this answer - https://stackoverflow.com/questions/16335591/spring-mvc-json-406-not-acceptable - it will help you. – Anish B. Oct 21 '19 at 05:47
0

You can achieve this using 2 approach

Approach 1: Set model attribute and using expression language you can find on jsp

model.addAttribute("test",true);

in Jsp page

${test}

Approach 2: If you are sending ajax request instead of ModelAndView create a object set any attribute boolean and return object from method @ResponseBody annotation you will get json in Ajax Response

@RequestMapping(value = REGISTERACTION , method = RequestMethod.POST)
public @ResponseBody MyCustomObject RegisterUser(@ModelAttribute("register") Register register,HttpServletRequest request, HttpServletResponse response)
{

    boolean Registrationsuccess = userService.RegisterUser(register);
    MyCustomObject cusobj=new MyCustomObject();
    cusobj.setStatus(true);
    return cusobj;
}

Whatever code you have written it will not return json(It is basically form submission approach) so you have to go with first approach.

Sudhir Ojha
  • 3,247
  • 3
  • 14
  • 24
vivekdubey
  • 484
  • 2
  • 7