Springboot - PUT和Delete方法无法识别

时间:2015-08-14 22:13:52

标签: spring jpa

我尝试重新创建本教程。http://g00glen00b.be/prototyping-spring-boot-angularjs/

Spring RESTContollers的GET和POST方法都在工作,但是我无法使PUT和DELETE方法工作。任何线索?

我整天坐在那里!。提前谢谢!

我发布了以下数据作为PUT(关于POST)

 {
        "id": 1,
        "checked": false,
        "description": "1st one"
    }

我收到以下错误:

.m.m.a.ExceptionHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
2015-08-14 15:10:11.981 DEBUG 35110 --- [io-8080-exec-10] .w.s.m.a.ResponseStatusExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
2015-08-14 15:10:11.981 DEBUG 35110 --- [io-8080-exec-10] .w.s.m.s.DefaultHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
2015-08-14 15:10:11.985  WARN 35110 --- [io-8080-exec-10] o.s.web.servlet.PageNotFound             : Request method 'PUT' not supported
2015-08-14 15:10:11.989 DEBUG 35110 --- [io-8080-exec-10] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2015-08-14 15:10:11.990 DEBUG 35110 --- [io-8080-exec-10] o.s.web.servlet.DispatcherServlet        : Successfully completed request

这里是主要代码: 包pwcore;

import java.util.*;


import org.springframework.beans.factory.annotation.*;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/items")
public class ItemController {


    @Autowired
    private ItemRepository repo;

    @RequestMapping(method= RequestMethod.GET)
    public List<Item> findItems(){
        return repo.findAll();
    }

    @RequestMapping(method= RequestMethod.POST)
    public Item addItem(@RequestBody Item item){
        item.setId(null);
        return repo.saveAndFlush(item);
    }

    @RequestMapping(value ="/{id}",method= RequestMethod.PUT)
    public Item updateItem(@RequestBody Item updatedItem,@PathVariable Integer id){
        updatedItem.setId(id);
        return repo.saveAndFlush(updatedItem);
    }

    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public void deleteItem(@PathVariable Integer id){
        repo.delete(id);
    }
}

这是JPA

package pwcore;

import org.springframework.data.jpa.repository.JpaRepository;

public interface ItemRepository extends JpaRepository<Item, Integer> {

}

这是实体

package pwcore;

import javax.persistence.*;

@Entity
@Table
public class Item {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;
    @Column
    private boolean checked;
    @Column
    private String description;

    public Integer getId(){
        return id;
    }
    public void setId(Integer id){
        this.id=id;
    }

    public boolean isChecked(){
        return checked;
    }
    public void setChecked(boolean checked){
        this.checked=checked;
    }

    public String getDescription(){
        return description;
    }
    public void setDescription(String description){
        this.description=description;
    }
}

0 个答案:

没有答案