当前位置: 首页 > 知识库问答 >
问题:

为实现dynamoDb函数而编写的Spring Boot应用程序的POST请求期间Postman上的错误404

宫俊远
2023-03-14

我的POST请求的正文是:-{“CustomerId”:“123”,“评级”:4,“金额购买”:1000,“金额取消”:100,“金额转向”:100,“罚款”:0,“DeliveryCharge”:true}我在发送POST请求时收到此错误:-{“时间戳”:“2020-04-26T11:32:57.940+0000”,“状态”:404,“错误”:“未找到”,“消息”:“无消息可用”,“路径”:“/Dynamodb”}POST URL是:-http://localhost:9003/Dynamodb

我附加了我的DynamodbController.java和DynamodBrepository.java和RatingSystem.java-这是我的模型类

package com.DDB.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.DDB.model.RatingSystem;
import com.DDB.repo.DynamodbRepository;

@RestController
@RequestMapping("/dynamoDb")
public class DynamodbController {

    @Autowired
    private DynamodbRepository repository;

    @PostMapping
    public String insertIntoDynamodb(@RequestBody RatingSystem brs) {
        repository.insertIntoDynamoDB(brs);
        return "Successfully inserted into DynamoDB table";
    }

    @GetMapping
    public ResponseEntity<RatingSystem> getOneCustomerDetails(@RequestParam int customerId) {
        RatingSystem brs = repository.getOneCustomerDetails(customerId);
        return new ResponseEntity<RatingSystem>(brs, HttpStatus.OK);
    }

    @PutMapping
    public void updateCustomerDetails(@RequestBody RatingSystem brs) {
        repository.updateCustomerDetails(brs);
    }

    @DeleteMapping(value = "{customerId}")
    public void deleteCustomerDetails(@PathVariable("customerId") String customerId) {
        RatingSystem brs = new RatingSystem();
        brs.setCustomerId(customerId);
        repository.deleteCustomerDetails(brs);
    }
}

接下来是存储库文件:-

package com.DDB.repo;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBSaveExpression;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.ComparisonOperator;
import com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException;
import com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue;
import com.DDB.model.RatingSystem;

@Repository
public class DynamodbRepository {

    private static final Logger LOGGER = LoggerFactory.getLogger(DynamodbRepository.class);

    @Autowired
    private DynamoDBMapper mapper;

    public void insertIntoDynamoDB(RatingSystem brs) {
        mapper.save(brs);
    }

    public RatingSystem getOneCustomerDetails(int customerId) {
        return mapper.load(RatingSystem.class, customerId);
    }

    public void updateCustomerDetails(RatingSystem brs) {
        try {
            mapper.save(brs, buildDynamodbSaveExpression(brs));
        } catch (ConditionalCheckFailedException exception) {
            LOGGER.error("invalid data - " + exception.getMessage());
        }
    }

    public void deleteCustomerDetails(RatingSystem brs) {
        mapper.delete(brs);
    }

    public DynamoDBSaveExpression buildDynamodbSaveExpression(RatingSystem brs) {
        DynamoDBSaveExpression saveExpression = new DynamoDBSaveExpression();
        Map<String, ExpectedAttributeValue> expected = new HashMap<>();
        expected.put("customerId", new ExpectedAttributeValue(new AttributeValue(brs.getCustomerId()))
                .withComparisonOperator(ComparisonOperator.EQ));
        saveExpression.setExpected(expected);
        return saveExpression;
    }
}

接下来是模型类:-

package com.DDB.model;

import java.io.Serializable;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAutoGeneratedKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;

@DynamoDBTable(tableName = "RatingSystem")
public class RatingSystem implements Serializable {

    private static final long serialVersionUID = 1L;

    private String customerId;
    private double rating;
    private double amountBought;
    private double amountCancelled;
    private double amountReturned;
    private double fine;
    private boolean deliveryCharge;


    @DynamoDBHashKey(attributeName = "customerId")
    @DynamoDBAutoGeneratedKey
    public String getCustomerId() {
        return customerId;
    }
    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }

    @DynamoDBAttribute
    public double getRating() {
        return rating;
    }
    public void setRating(double rating) {
        this.rating = rating;
    }

    @DynamoDBAttribute
    public double getAmountBought() {
        return amountBought;
    }
    public void setAmountBought(double amountBought) {
        this.amountBought = amountBought;
    }

    @DynamoDBAttribute
    public double getAmountCancelled() {
        return amountCancelled;
    }
    public void setAmountCancelled(double amountCancelled) {
        this.amountCancelled = amountCancelled;
    }

    @DynamoDBAttribute
    public double getAmountReturned() {
        return amountReturned;
    }
    public void setAmountReturned(double amountReturned) {
        this.amountReturned = amountReturned;
    }

    @DynamoDBAttribute
    public double getFine() {
        return fine;
    }
    public void setFine(double fine) {
        this.fine = fine;
    }

    @DynamoDBAttribute
    public boolean isDeliveryCharge() {
        return deliveryCharge;
    }
    public void setDeliveryCharge(boolean deliveryCharge) {
        this.deliveryCharge = deliveryCharge;
    }

}

我已经在AWS dynamodb上创建了这个表,其属性与模型类中的属性相同。这个spring boot应用程序也成功地运行了,没有任何错误。控制台运行时如下所示:-

2020-04-26 17:16:34.465信息9584---[main]O.A.C.C.C.[Tomcat].[localhost].[/]:初始化Spring embedded WebApplicationContext

2020-04-26 17:16:34.465信息9584---[main]o.s.web.context.contextLoader:Root WebApplicationContext:初始化在792毫秒内完成

2020-04-26 17:16:34.629信息9584---[main]O.S.Concurrent.ThreadPoolTaskExecutor:初始化ExecutorService“Application TaskExecutor”

2020-04-26 17:16:34.787信息9584---[main]com.example.demo.DdbApplication:用1.536秒启动DdbApplication(JVM运行时间为2.358)

共有1个答案

楚硕
2023-03-14

除了@SpringBootApplication之外,我还在ddbApplication.java文件中添加了@ComponentScan和@EnableAutoConfiguration注释-这个java文件中有主类。

 类似资料:
  • 当我尝试向邮递员发送邮件请求时,仍然会出现这个错误。 我可以从我的DRFlocalhost成功地发出相同的帖子请求,但是当我尝试邮递员时,我得到了上面的错误。 我该怎么解决呢? Views.py Serializers.py urls.py 项目urls.py 更新 我根据评论中的错误消息和指南进行了一些更改,现在可以创建用户了。 问题是,在发送表单中的用户凭据后,我在postman中收到了这个错

  • 大家好,我是新来的AWS,正在尝试在Elastic Beanstalk中部署spring boot应用程序。部署成功,但无法访问应用程序。获取502错误请求网关nginx/1.10.1错误。部署在64位Amazon Linux 2016.09 v2配置上完成。2.0运行Java 8(nginx代理服务器)。有没有办法解决这个问题。在谷歌搜索之后,我发现这是因为端口不匹配。我想知道我应该在哪里更改端

  • 每当页面尝试提交2MB或更大的文件时,都会发生此错误。但是参数在帖子里!我已经检查了Chrome开发工具。有人知道这个错误吗?Springboot 2.0.3。释放

  • 如何通过postman发送应用程序/json数据,以便在nodejs服务器中接收与req相同的数据。身体我在postman中尝试了原始body json,它以application/json的形式发送数据,但数据在服务器中以req的形式提供。罗博迪。我需要同样的东西。身体有这样的选择吗

  • 我已经用谷歌搜索了它。但是这些都不能解决我的问题。我在调用rest控件时,代码中的SSL证书出现了问题。 我的控制方法是: 错误为:-I/O错误在“https://myurl.com”的POST请求上:sun.security.validator.validatoreXception:PKIX路径构建失败:sun.security.provider.certpath.suncertPathBuil

  • 我收到 400 个错误的 AJAX 发布方法请求。我正在后端使用Spring数据Rest服务。以下是我在JS前端的代码 尽管我正在序列化 JSON 数据。我仍然收到400错误请求错误。如果后端的某些代码中断或发送到服务器的请求出现问题,则会出现此错误吗? JAVA实现