Home > OS >  Http POST on rest controller returns nothing Springboot
Http POST on rest controller returns nothing Springboot

Time:05-31

I want to make a simple HTTP POST request using REST API in Springboot.

HTTP GET have been already implemented and they work fine, I'm following this link but I got stuck on POST method.

I think RestController is right, but I have some doubts about the request that I'm sending to Springboot using terminal.

First of all, this is the model that I want to save on db (Postgres) using POST method, it's called NFT :

@Entity
public class NFT {
    
    public NFT(Long new_user_id) {
        this.title=getRandomString();
        this.price=0;
        this.description="Niente";
        this.image="";
        this.user_id=new_user_id;
    }
    
    public NFT() {
        this.title=getRandomString();
        this.price=0;
        this.description="Niente";
        this.image="";
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private Long user_id;
    
    public String title;
    
    public String image;
    
    public float price;

    public String description;
    
}

This class has 2 constructors, one using User_ID and the other is just an empty default constructor so I can make empty post request because ID will be autogenerated.

This is my rest class :

@RestController
@RequestMapping("/restNFT")
public class NFTRestController {
        
    @Autowired
    private NFTService service;
    
    //curl GET http://127.0.0.1:8080/restNFT/1   example
    
    @RequestMapping(value="/{nft_id}",method=RequestMethod.GET)
    public Optional<NFT> getNFTbyID(@PathVariable int nft_id) {
        return service.getSingleNFTbyId((long) nft_id);
    }
    
    //curl GET http://127.0.0.1:8080/restNFT/user/1
    @RequestMapping(value="/user/{persona_id}",method=RequestMethod.GET)
    public List<NFT> getAllNFTsFromPerson(@PathVariable int persona_id) {
        return service.getAllNFTsFromPerson((long) persona_id);
    }
    
    @RequestMapping(value="/nft",method=RequestMethod.POST)
    public void saveNFT(@RequestBody  NFT nft) {
        System.out.println(nft);
        service.saveNewNFT(nft);
    }
    
    @RequestMapping(value="/manyNFTs",method=RequestMethod.POST)
    public void saveAllNFTs(List<NFT> nfts) {
        service.saveAllNFT(nfts);
        return ;
    }
    
}

N.B. : service.saveNewNFT(nft) just calls save() method of CrudRepository interface.

GET requests work so I think that at least the first half of this class is correct, for example writing on terminal :

curl GET http://127.0.0.1:8080/restNFT/1 

Returns correctly :

{"title":"yonwqelnrx","image":"","price":0.0,"description":"Nothing"}

But If I try to make an empty POST request :

curl POST http://127.0.0.1:8080/restNFT/nft -d '{}'

I expect a json as response from db, instead nothing is printed on screen.

EDIT : Edited two functions :

@Transactional
    public NFT saveNewNFT(@RequestBody NFT nft) {   
        return nr.save(nft);
    }

And :

@RequestMapping(value="/nft",method=RequestMethod.POST)
    public NFT saveNFT(@RequestBody  NFT nft) {
        return service.saveNewNFT(nft) ;
    }

Still testing with : curl -X POST http://127.0.0.1:8080/restNFT/nft -H 'Content-type:application/json' -d '{}' the result is the same, nothing changed.

CodePudding user response:

The method you've mapped to that call, saveNFT, has a return type of void. Therefore, nothing (void) is returned in the response body.

Once you return the saved NFT, that will become the response body.

  1. Update NFTService#saveNewNFT(NFT) to return the result of CrudRepository#save(NFT) -- this will return the persisted instance of the NFT (you might already be doing this)
  2. Update NFTRestController#saveNFT(NFT) to return the result of service.saveNewNFT(nft) -- this will make the saved NFT the response body of your endpoint

CodePudding user response:

Solved, the problem was inside security configuration class, I forgot to provide permissions for both GET and POST operations on "/restNFT/**" path :

public class AuthConfiguration extends WebSecurityConfigurerAdapter {

@Override
    protected void configure(HttpSecurity http) throws Exception {
    ...//some code
    .antMatchers(HttpMethod.GET, "/","/restNFT/**").permitAll()
    .antMatchers(HttpMethod.POST,"/restNFT/**").permitAll()
  • Related