Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i am new to webservice through spring bot. When i use @GetMapping method it works fine there is no issue,and i provide @DeleteMapping methoed it throw mw this type of error
Request method "GET" not supported. how to solve this issue.

i check the url through my browser and post man everywhere i got same error

What I have tried:

controller.java
Java


package com.main.AngBoot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.main.AngBoot.bean.Product;
import com.main.AngBoot.service.ProductHardcodedService;
@CrossOrigin(origins="http://localhost:4200")
@RestController
public class ProductController {
	@Autowired
	private ProductHardcodedService prodService;
	
	@GetMapping("/users/{productname}/prodct")
	public List<Product> getAllProducts(@PathVariable String productname){
		return prodService.findAll();
		
	}
	@DeleteMapping("/users/{productname}/prodct/{id}")
	public ResponseEntity<Void> deleteProduct(@PathVariable String productname,
			@PathVariable long id){
		Product product = prodService.deleteById(id);
		if (product != null) {
			return ResponseEntity.noContent().build();
		}
		return ResponseEntity.notFound().build();
	}

}

hardcodeservice.java
package com.main.AngBoot.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.main.AngBoot.bean.Product;

@Service
public class ProductHardcodedService {
	private static List<Product>prodct = new ArrayList<>();
	private static int idCounter=0;
	
	static{
//		prodct.add(new Product(new Product(++idCounter,"LAPTOP",10000.00,new Date(),new Date(),"delivired")));
		prodct.add(new Product(++idCounter, "Laptop", 1000.00, new Date(), new Date(), false));
		prodct.add(new Product(++idCounter, "Printer", 150.00, new Date(), new Date(), true));
		prodct.add(new Product(++idCounter, "Cartage", 100.00, new Date(), new Date(), false));
	}
	public List<Product> findAll(){
		return prodct;
		
	}
	public Product deleteById(long id){
		Product product = findById(id);
		if(product==null)
			return null;
		if(prodct.remove(product)){
			return product;
		}
		return null;
	}
	public Product findById(long id) {
		// TODO Auto-generated method stub
		for (Product product : prodct) {
			if(product.getId()==id){
				return product;
			}
		}
		return null;
	}

}


product.java
package com.main.AngBoot.bean;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;

import lombok.Data;

@Entity
public class Product {
	@Id
	private long id;
	private String productname;
	private Double price;
	private Date orderDate;
	private Date deliverDate;
	private boolean isDone;
	public Product(long id, String productname, Double price, Date orderDate, Date deliverDate, boolean isDone) {
		super();
		this.id = id;
		this.productname = productname;
		this.price = price;
		this.orderDate = orderDate;
		this.deliverDate = deliverDate;
		this.isDone = isDone;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (id ^ (id >>> 32));
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Product other = (Product) obj;
		if (id != other.id)
			return false;
		return true;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getProduct() {
		return productname;
	}
	public void setProduct(String product) {
		this.productname = product;
	}
	public Double getPrice() {
		return price;
	}
	public void setPrice(Double price) {
		this.price = price;
	}
	public Date getOrderDate() {
		return orderDate;
	}
	public void setOrderDate(Date orderDate) {
		this.orderDate = orderDate;
	}
	public Date getDeliverDate() {
		return deliverDate;
	}
	public void setDeliverDate(Date deliverDate) {
		this.deliverDate = deliverDate;
	}
	public boolean isDone() {
		return isDone;
	}
	public void setDone(boolean isDone) {
		this.isDone = isDone;
	}
	
	
	
	

}
Posted
Comments
Sandeep Mewara 24-Aug-20 3:23am    
Try: ResponseEntity<Long> as return type for deleteProduct
Sandeep Mewara 24-Aug-20 3:27am    
Also,
Also, try these return types:

return new ResponseEntity<>(HttpStatus.NOT_FOUND);

return new ResponseEntity<>(id, HttpStatus.OK);
ZurdoDev 24-Aug-20 10:10am    
It means your delete method is set to use post, not get.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900