I have created a Restfull webservice using spring boot to add a record to H2 database but when I send data from postman to my handler method I get null values on the server side and on the response sent to the client side as well, could anybody help me? [P My Eclipse console snapshot ostman snapshot] 2
My Controller:
@RestController
public class AlienController {
@Autowired
AlienRepo repo;
@RequestMapping("/")
public String home() {
return"home.jsp";
}
@PostMapping(path="/alien")
public Alien addAlien(Alien alien) {
System.out.println(alien);
repo.save(alien);
return alien;
}
My DAO Class:
@Entity
public class Alien {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int aid;
private String aname;
private String lang;
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid=aid;
}
public String getAname() {
return aname;
}
public void setAname(String aname) {
this.aname=aname;
}
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang=lang;
}
@Override
public String toString() {
return "Alien Info: Aid=" + aid + ", Aname=" + aname + ", Lang=" +lang;
}
}
My AlienRepository:
public interface AlienRepo extends JpaRepository<Alien, Integer>{
}
You should use the annotation @RequestBody:
public Alien addAlien(@RequestBody Alien alien)
You should inform Spring that you are waiting for a HttpRequest that contains a Body, Spring will automatically deserialize the inbound HttpRequest body onto Java object.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.