Chandler Bing
Chandler Bing

Reputation: 343

spring-boot:Object as API request getting null

I am new to spring boot and i am getting null object that i am passing from frontend reactjs, my code is as follows,

package com.example.demo;
import org.springframework.web.bind.annotation.*;
import com.example.pojos.User;

@RestController
@RequestMapping(value="/askmeanything")
public class HelloController {
  @CrossOrigin(origins = "*")
  @RequestMapping(value = "/login", method = RequestMethod.POST)
    public void getMsg(User user) throws  Exception {
        System.out.println("********"+user.getUserName()+"*********");
    }
}

and reactjs code

axios.post('http://localhost:8080/askmeanything/login/', {
        userName: this.state.userName,
        password: this.state.password
    }).then(function(response) {
        console.log(response);
    })
    .catch(function(error) {
        console.log(error);
    });

Upvotes: 2

Views: 327

Answers (1)

Yogesh Prajapati
Yogesh Prajapati

Reputation: 4870

Change line

public void getMsg(User user) throws  Exception {

to

public void getMsg(@RequestBody User user) throws  Exception {

Added @RequestBody.

Upvotes: 1

Related Questions