Тело моего POST-запроса: - {"customerId": "123", "rating": 4, "amountBought": 1000, "amountCancelled": 100, "amountReturned": 100, "fine": 0, " deliveryCharge ": true} Я получаю эту ошибку при отправке запроса POST: - {" timestamp ":" 2020-04-26T11: 32: 57.940 + 0000 "," status ": 404," error ":" Not Found ", "message": "Сообщение недоступно", "path": "/ DynamoDb"} URL-адрес POST: - 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 с теми же атрибутами, что и в классе модели. Это приложение с весенней загрузкой также успешно работает, без ошибок. При запуске консоль выглядит примерно так: -
2m2020-04-26 17: 16: 33.636 INFO 9584 --- [main] com.example.demo.DdbApplication: запуск приложения DdbApplication для LAPTOP-INEF3PKK с PID 9584 (C: \ Users \ yoin \ Documents \ workspace-spring-tool-suite-4-4.6.0.RELEASE \ DDB \ target \ классы, запущенные yoin в C: \ Users \ yoin \ Documents \ workspace -spring-tool-suite-4-4.6.0.RELEASE \ DDB)
2020-04-26 17: 16: 33.639 INFO 9584 --- [main] com.example.demo.DdbПрименение: нет активный профиль установлен с использованием профилей по умолчанию: по умолчанию
2020-04-26 17: 16: 34.367 INFO 9584 --- [main] osbwembedded.tomcat.TomcatWebServer: Tomcat, инициализированный с портом (портами): 9003 (http)
2020-04-26 17: 16: 34.374 INFO 9584 --- [main] o. apache .catalina.core.StandardService: Запуск службы [Tomcat]
2020-04-26 17: 16: 34.375 INFO 9584 --- [main] org. apache .catalina.core.StandardEngine: Запуск двигателя сервлета: [Apache Tomcat / 9.0.33]
2020-04-26 17: 16: 34.465 ИНФОРМАЦИЯ 9584 --- [main] oa c. c. C. [Tomcat]. [Localhost]. [/]: Инициализация встроенного в Spring WebApplicationContext
2020-04-26 17: 16: 34.465 INFO 9584 --- [main] osweb.context.ContextLoader: Root WebApplicationContext: инициализация завершена за 792 мс
2020-04-26 17: 16: 34.629 INFO 9584 --- [main ] ossconcurrent.ThreadPoolTaskExecutor: Инициализация ExecutorService 'applicationTaskExecutor'
2020-04-26 17: 16: 34.785 INFO 9584 --- [main] osbwembedded.tomcat.TomcatWebServer: Tomcat запущен на порте (ах): 9003 ( http) с контекстным путем ''
2020-04-26 17: 16: 34.787 INFO 9584 --- [main] com.example.demo.DdbApplication: запуск приложения DdbApplication через 1,536 секунды (JVM работает в течение 2,385)