我想显示以特定ID开头的消息,例如,我选择ID =4。可以。
curl -H "Content-Type: application/json" localhost:8080/api/unread/4
但是告诉我卷曲请求的写法不正确,他应该发送Json并返回Json,对不起,也许我写的示例不正确,但是它看起来应该像这样-
curl -H "Content-Type: application/json" -d "{id=4"}" localhost:8080/api/unread
RestService
@Service
public class RestService {
private final RestTemplate restTemplate;
public RestService(RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder.build();
}
public Message saveMessage(Message message) {
String url = "http://localhost:8080/api/save";
return this.restTemplate.postForObject(url, message, Message.class);
}
public void updateMessage(long id, Message message) {
String url = String.format("http://localhost:8080/api/update/%d", id);
this.restTemplate.put(url, message);
}
public List<Message> getLast() {
String url = "http://localhost:8080/api/last";
String json = restTemplate.getForObject(url, String.class);
return new Gson().fromJson(json, new TypeToken<List<Message>>(){}.getType());
}
}
RestController
@org.springframework.web.bind.annotation.RestController
public class RestController {
@Autowired
TimerTask timerTask;
@Resource
private final MessageService messageService;
public RestController(MessageService messageService) {
this.messageService = messageService;
}
@PostMapping("/api/save")
public ResponseEntity<String> 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);
}
MessageServiceImpl
@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 ResponseEntity<String> add(@RequestBody Message message) {
try {
message.setTime(new Timestamp(new Date().getTime()));
repository.save(message);
return new ResponseEntity<>("Сообщение сохранено", HttpStatus.OK);
}catch (Exception e) {
return new ResponseEntity<>(e.toString(), HttpStatus.CONFLICT);
}
}
@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);
}
}
}
尝试使用以下卷曲请求。
curl -X GET --header 'Accept: application/json' 'http://localhost:8080/api/unread/4'