请求方法'POST'不支持-Spring Boot

时间:2019-12-06 11:11:33

标签: spring spring-boot

我试图这样 POST curl -X POST -H "Content-Type: application/json" -d "{ \"fisrtName\":\"sunny\" , \"lastName\":\"leone\" }" http://localhost:8080/user

但我收到此错误: {“时间戳”:“ 2019-12-06T11:07:17.196 + 0000”,“状态”:405,“错误”:“不允许使用方法”,“消息” :“不支持请求方法'POST'”,“路径”:“ /用户”}

但是,我有这样的休息控制器:

@PostMapping("/user/{firstName}/{lastName}")
    User addUser(@PathVariable String firstName,@PathVariable String lastName)
    {
        return userRepository.save(new User(firstName,lastName));
    }

如何解决?

2 个答案:

答案 0 :(得分:4)

您正在将firstName和lastName作为JSON传递。

但是您的映射说它们是路径变量:

@PostMapping("/user/{firstName}/{lastName}")

现在,您尝试访问http://localhost:8080/user,但此路径没有映射。

您的帖子映射应为:

@PostMapping("/user")
User addUser(@RequestBody User user){
    return userRepository.save(user);
}

答案 1 :(得分:0)

我希望这会有所帮助。 如果您使用@RequestBody而不是pathvariable会有所帮助。

library(shinydashboard)
library(readxl)
out <- data.frame(baseFns = ls('package:base'))
ui <- dashboardPage(
  dashboardHeader(title = "Loading data"),
  dashboardSidebar(sidebarMenu(
    menuItem("Load Data", tabName = "Load_Data", icon = icon("balance-scale")),
    menuItem("Analysis", tabName = "Analysis", icon = icon("chart-bar"))
  )),
  dashboardBody(
    tabItems(tabItem(tabName = "Load_Data",textInput("T", "No of data sets", value = 0,width = 150),
                     fluidRow(box(fileInput("datafile","Choose the csv file",multiple = TRUE, 
                                            accept = c("text/csv","text/comma-separated-values,text/plain",".csv")),width = 2)
                              ),
                     fluidRow(box(title = "Dataset",uiOutput("filter_70"),width = 5000)))
    ))
)

server <- function(input,output){
  output$contents <- renderTable({
    file_to_read <- input$datafile
    if(is.null(file_to_read))
      return(NULL)
    a <- read.csv(file_to_read$datapath)
    head(a,n=15)
  })

}
shinyApp(ui, server)
@PostMapping("/user)
    User addUser(@RequestBody User user)
    {
        if(user==null){
        return null;
        }else
        return userRepository.save(user);
    }