[英]Curl command not working (Spring Boot project)
I want to display all messages from MySQL database that start with id = 275. I wrote a curl request I don't know.我想显示来自 MySQL 数据库的所有以 id = 275 开头的消息。我写了一个我不知道的 curl 请求。 Maybe the request was written incorrectly.
也许请求写错了。 Maybe the query syntax made a mistake -
也许查询语法出错了 -
curl -H "Content-Type: application/json" -d "{ \"messageId\": \"275\" }" localhost:8080/api/unread
But when I run this curl request through the command line, it shows an error.但是当我通过命令行运行这个 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(
My code Input Message class我的代码输入消息类
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;
}
}
RestController休息控制器
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);
}
Message Service Impl消息服务实现
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 github https://github.com/fallen3019/vaadin-chat
Looking at your controller and curl request, you are calling /api/unread
which is declared as GET
request in your RestController#getUnreadMessages()
.查看您的控制器和 curl 请求,您正在调用
/api/unread
,它在您的RestController#getUnreadMessages()
声明为GET
请求。
Guessing you meant to hit POST /api/save
, in that case simply change the curl request to following curl -H "Content-Type: application/json" -d "{ \\"messageId\\": \\"275\\" }" localhost:8080/api/save
猜测您打算点击 POST
/api/save
,在这种情况下,只需将 curl 请求更改为 curl curl -H "Content-Type: application/json" -d "{ \\"messageId\\": \\"275\\" }" localhost:8080/api/save
If you meant to call /api/unread
, as this is GET request that doesn't require any params, you can simply call it as curl -X GET "localhost:8080/api/unread" -H "Content-Type: application/json"
如果您打算调用
/api/unread
,因为这是不需要任何参数的 GET 请求,您可以简单地将其称为curl -X GET "localhost:8080/api/unread" -H "Content-Type: application/json"
It is pretty clear it thinks you execute a post call not a get call.很明显,它认为您执行的是 post 调用而不是 get 调用。 Please specify
-X GET
and keep the data in -d as you already do.请指定
-X GET
并将数据保存在 -d 中,就像您已经做的那样。 I'm pretty sure curl assumes -X with -d is POST by default if no otherwise specified.如果没有另外指定,我很确定 curl 假设 -X 和 -d 默认是 POST 。
As per the program code presented, the endpoint /api/unread
does only support GET
, not POST
.根据提供的程序代码,端点
/api/unread
仅支持GET
,不支持POST
。 To set off a GET
via curl, add -X GET
:要通过 curl 启动
GET
,请添加-X GET
:
curl -H "Content-Type: application/json" \
# -d "{ \"messageId\": \"275\" }" \ # as of now, the body is not used and can be commented-out
-X GET \
-v \ # enable debug output to see response headers
localhost:8080/api/unread
Or, alternatively, change the endpooint to /api/save
:或者,将端点更改为
/api/save
:
curl -H "Content-Type: application/json" \
-d "{ \"messageId\": \"275\" }" \
localhost:8080/api/save
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.