org.springframework.web.httprequestmethodnotsupportedexception:请求方法'get'不受支持]

eqoofvh9  于 2021-06-27  发布在  Java
关注(0)|答案(3)|浏览(747)

我提出请求时遇到了问题”http://localhost:8080/main/home“-出现意外错误 (type=Internal Server Error, status=500). 当一个请求http://localhost:8080/main/add“-出现意外错误 (type=Method Not Allowed, status=405). 我的控制器类

package com.example.transactions.controller;

 import com.example.transactions.model.Transaction;
 import com.example.transactions.repository.TransactionRepository;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;

 import java.util.List;

 @RestController
 @RequestMapping("/main")
 public class TransactionController {

    @Autowired (required = false)
    private TransactionRepository transactionRepository;

    @GetMapping("/home")
    public List<Transaction> getAll(){
        return transactionRepository.findAll();
    }

    @PostMapping("/add")
    public Transaction addTransaction (@RequestParam Transaction transaction ){
        return transactionRepository.save(transaction);
    }
}

我的存储库类

package com.example.transactions.repository;

import com.example.transactions.model.Transaction;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface TransactionRepository extends JpaRepository<Transaction,Integer> {
}

应用程序属性

spring.jpa.show-sql=true
spring.jab.hibernate.ddl-auto=update

spring.datasource.url=jdbc:oracle:thin:@localhost:1521/oracle
spring.datasource.username=sys
spring.datasource.password=chingishan
spring.datasource.initialization-mode=always
3yhwsihp

3yhwsihp1#

关于你的第一期:错误500。
spring无法注入依赖项:transactionrepository
设置autowiredasrequired=false不是一个好的做法,因为如果不解决依赖关系,spring将使bean处于未受保护的状态,而不是在调用http://localhost:8080/main/home”。
请共享你的应用程序日志,并删除此日志。你的应用程序将在启动期间开始失败,因此你可以得到正确的错误。

7rfyedvj

7rfyedvj2#

请检查application.properties文件配置

spring.jpa.properties.hibernate.show_sql=true
spring.jpa.hibernate.ddl-auto=update
spring.data.jpa.repositories.enabled=true
spring.datasource.url=jdbc:oracle:thin:@localhost:1521/oracle
spring.datasource.username=sys
spring.datasource.password=chingishan
spring.datasource.initialization-mode=always

在这里检查。它应该是对象的requestbody和用于名称-值对的requestParams。

@PostMapping("/add")
    public Transaction addTransaction (@RequestBody Transaction transaction ){
        return transactionRepository.save(transaction);
    }

获取事务数据并将其设置为实体,然后调用repository save方法。

7xzttuei

7xzttuei3#

因为只有一个 @PostMapping 为了 /main/add ,您的请求 http://localhost:8080/main/add 不支持可能是get的。
至于另一个端点的500:这意味着您的代码抛出 RuntimeException 这是不可处理的。您应该看到堆栈跟踪。除此之外,这个问题没有足够的细节。

相关问题