gpt4 book ai didi

java - Curl 命令不起作用(Spring Boot 项目)

转载 作者:行者123 更新时间:2023-12-02 09:08:25 25 4
gpt4 key购买 nike

我想显示MySQL数据库中以id = 275开头的所有消息。我写了一个我不知道的curl请求。也许请求写得不正确。也许查询语法出错 -

curl -H "Content-Type: application/json" -d "{ \"messageId\": \"275\" }" localhost:8080/api/unread

但是当我通过命令行运行这个curl请求时,它显示了一个错误。

{"timestamp":"2020-01-06T03:13:07.899+0000","status":405,"error":"Method Not Allowed","message":"Request method 'POST' not supported","trace":"org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported\r\n\tat org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(

我的代码输入消息类

public class InputMessage {

private long messageId;

public InputMessage(long messageId) {
this.messageId = messageId;
}

public InputMessage() {
}

public long getMessageId() {
return messageId;
}

public void setMessageId(long messageId) {
this.messageId = messageId;
}
}

休息 Controller

public class RestController {

@Autowired
TimerTask timerTask;

@Resource
private final MessageService messageService;

public RestController(MessageService messageService) {
this.messageService = messageService;
}

@PostMapping("/api/save")
public MessageStatus saveMessage(@RequestBody Message chatMessage) {
return messageService.add(chatMessage);
}

@GetMapping("/api/last")
public String getLasts() {
return new Gson().toJson(messageService.getLast());
}

@GetMapping("/api/unread")
public void getUnreadMessages() {

timerTask.run();
}

@GetMapping("/api/unread/byid")
public List<Message> getUnreadById(@RequestBody InputMessage message) {
return messageService.getUnreadById(message);
}

消息服务实现

public class MessageServiceImpl implements MessageService {
private final MessageRepository repository;
private final PageRequest lastRequest;

private List<Long> chekedMessages = new ArrayList<>();


@Autowired
public MessageServiceImpl(MessageRepository repository) {
this.repository = repository;
lastRequest = new PageRequest(0, 10, Sort.Direction.DESC, "id");
}

@Override
public MessageStatus add(@RequestBody Message message) {
if(message == null) {
System.out.println("Пришел пустой запрос на сохранение данных");

}
MessageStatus status = new MessageStatus();
try {
repository.save(message);
status.setMessage("Сообщение успешно сохранено");
}
catch (Exception e) {
status.setMessage("Во время сохранения сообщения произошла ошибка");
}
return status;
}

@Override
public List<Message> getAllMessages() {
return repository.findAll();
}

@Override
public List<Message> getLast() {
List<Message> result = repository.findAll(lastRequest).getContent();

return result.stream()
.sorted(Comparator.comparingLong(Message::getId))
.collect(Collectors.toList());
}

@Override
public List<Message> getUnreadById(InputMessage message) {
return repository.getUnreadById(message.getMessageId());
}



@Override
public String getUnreadMessages() {
List<Message> out = new ArrayList<>();
List<Message> unchekedMessages = repository.findAll();
for (Message message: unchekedMessages) {
if (!chekedMessages.contains(message.getId())) {
chekedMessages.add(message.getId());
out.add(message);
}
}
return new Gson().toJson(out);
}

@Override
public void updateMessage(long id, Message message) {
if (repository.findById(id).isPresent()) {
message.setId(id);
repository.save(message);
}
}

github https://github.com/fallen3019/vaadin-chat

最佳答案

查看您的 Controller 和curl请求,您正在调用/api/unread声明为 GET在您的 RestController#getUnreadMessages() 中请求.

我猜你是想按 POST /api/save ,在这种情况下只需将curl请求更改为以下 curl -H "Content-Type: application/json" -d "{ \"messageId\": \"275\" }" localhost:8080/api/save

如果您想调用/api/unread ,因为这是不需要任何参数的 GET 请求,您可以简单地将其称为 curl -X GET "localhost:8080/api/unread" -H "Content-Type: application/json"

关于java - Curl 命令不起作用(Spring Boot 项目),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59606309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com