1

So I am using axios.post on React here is my code

React GenService.jsx

import axios from "axios";
import { Route, withRouter } from "react-router-dom";


const COURSE_API_URL = "http://localhost:8080";


class GenService {
  retrieveAll() {
    return axios.get(`${COURSE_API_URL}`);
  }
  pAll() {
    return axios.post(`${COURSE_API_URL}` + "/user", {
      firstName: "Fred",
      lastName: "Flintstone"
    });
  }
}

export default new GenService();

But I can't figure out how to call firstName and lastName in backend java using spring boot

SpringBootLd2nlApplication.java

package com.ld2nl.springbootld2nl;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@SpringBootApplication
@CrossOrigin(origins = { "http://localhost:3000", "http://localhost:4200" })
@RestController

public class SpringBootLd2nlApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootLd2nlApplication.class, args);
    }
    //works
    @RequestMapping(value = "/")
    public String say() {
        return "Sending Hello To React";
    }

//not work
    @RequestMapping(value = "/user")
    public String reply(@RequestParam String firstName) {
        return firstName;
    }

}

As you can see can't get the area marked as "//not work" to properly call firstName and lastName

Currently giving error

Required String parameter 'firstName' is not present
....
1

3 Answers 3

3

Instead of trying to directly fetch the parameter which won't work as POST sends data as a payload/JSON object rather than split parameter-wise, below should work:

  @PostMapping(value = "/user")
   public String reply(@RequestBody Map<String, Object> payLoad) {
       return (String)payLoad.get("firstName");
   }
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of passing firstname like that, try creating a const for firstname.

const firstName = "Fred";
...
...
pAll(){
     return axios.post(http://localhost:8080/users/${firstname});
}

And in the Springboot, add these lines in the reply() function

@GetMapping("/users/{firstname}
public String reply(@PathVariable String firstName) {
    return firstName;
}

Comments

0

The best way (production way) is to provide separate DTO.

   public class SendMessageDto {
     @NotBlank @Length(min=4)...
     private String userName;
     public String getUserName() {return this.userName} ;
   }

In controller

   @PostMapping(value = "/user")
   public String reply(@Valid @RequestBody SendMessageDto) { //
       return payLoad.getUserName();
   }
  • @RequestBody - is optional in this context.
  • @Valid - needed for request validation

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.