05.01.2020, 07:56
#39910870
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
|
|
|
Когда я запускаю этот cURL запрос
1.
curl -H "Content-Type: application/json" -X POST -d "{\"time\": \"2019-12-27T09:50:02.000+0000\", \"unread\": true, \"message\": \"asd\", \"from\": \"John\"}" localhost:8080/api/save
У меня вот так ошибка
1.
{"timestamp":"2020-01-05T04:36:33.192+0000","status":500,"error":"Internal Server Error","message":"No message available","trace":"java.lang.NullPointerException\r\n\tat org.vaadin.marcus.spring.service.impl.MessageServiceImpl.add(MessageServiceImpl.java:45)\r\n\tat org.vaadin.marcus.spring.service.impl.MessageServiceImpl$$FastClassBySpringCGLIB$$2d73a7c.invoke(<generated>)\r\n\tat org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)\r\n\tat org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746)\r\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)\r\n\tat org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)\r\n\tat org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)\r\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)\r\n\tat org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept
Вот мой код
MessageServiceImpl
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70.
@Service
@Transactional
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) {
MessageStatus status = null;
try {
repository.save(message);
status.setMessage("Сообщение успешно сохранено");
}
catch (Exception e) {
status.setMessage("Во время сохранения сообщения произошла ошибка - " + e);
}
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(long id) {
return repository.getUnreadById(id);
}
@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);
}
}
}
restcontroller
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32.
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/{id}")
public List<Message> getUnreadById(@PathVariable ("id") long id) {
return messageService.getUnreadById(id);
}
|
|