I’m trying to create delete function with Spring boot, reactjs and axios.
For first step, I just confirm if delete action is activated by entering URL directly.
But It doesn’t work even if I enter URL directly.
I know GET is not supported but I don’t know which I should fix.
Please tell me if you know.
ActionController.java
package com.example.demo.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.example.demo.repository.CheckListRepository; import com.example.demo.service.CheckListService; @CrossOrigin @RequestMapping("action/") @RestController public class ActionController { @Autowired CheckListRepository clr; @Autowired CheckListService cls; @DeleteMapping(path = "{deleteId}") public void deleteAction(@PathVariable Integer deleteId) { clr.deletebyListNo(deleteId); } }
PS: This is axios code about contents. Controller
package com.example.demo.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.example.demo.entity.CheckList; import com.example.demo.entity.CheckListForm; import com.example.demo.repository.CheckListRepository; import com.example.demo.service.CheckListService; @CrossOrigin @RestController @RequestMapping("api/") public class CheckListController { @Autowired private CheckListRepository checkListRepository; @Autowired private CheckListService cls; @GetMapping("list") public List<CheckListForm> getList() { List<CheckList> checkList = this.checkListRepository.findAll(); List<CheckListForm> checkListForm = cls.entityToForm(checkList); return checkListForm; } }
CheckList.js
import axios from 'axios' const CHECKLIST_REST_API_URL = 'http://localhost:8080/api/list'; class CheckListService { getList() { return axios.get(CHECKLIST_REST_API_URL); } } export default new CheckListService();
Answer
I could confirm delete action method in control is activated in the following way. I changed code
package com.example.demo.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.example.demo.repository.CheckListRepository; import com.example.demo.service.CheckListService; @CrossOrigin @RequestMapping("action/") @RestController public class ActionController { @Autowired CheckListRepository clr; @Autowired CheckListService cls; @DeleteMapping(path = "{deleteId}") public void deleteAction(@PathVariable Integer deleteId) { clr.deletebyListNo(deleteId); } }
to
package com.example.demo.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.example.demo.repository.CheckListRepository; import com.example.demo.service.CheckListService; @CrossOrigin @RequestMapping("action/") @RestController public class ActionController { @Autowired CheckListRepository clr; @Autowired CheckListService cls; @RequestMapping(path = "{deleteId}") public void deleteAction(@PathVariable Integer deleteId) { clr.deletebyListNo(deleteId); } }