Java 通过cURL(json)显示消息(springboot)
我想显示以特定id开头的消息,例如,我选择了id=4。它起作用了Java 通过cURL(json)显示消息(springboot),java,json,spring,spring-boot,Java,Json,Spring,Spring Boot,我想显示以特定id开头的消息,例如,我选择了id=4。它起作用了 curl -H "Content-Type: application/json" localhost:8080/api/unread/4 但是告诉我curl请求写得不正确,他应该发送Json并返回Json,对不起,也许我写的示例不正确,但应该是这样的- curl -H "Content-Type: application/json" -d "{id=4"}" localhost:8080/api/unread 再服务 @Ser
curl -H "Content-Type: application/json" localhost:8080/api/unread/4
但是告诉我curl请求写得不正确,他应该发送Json并返回Json,对不起,也许我写的示例不正确,但应该是这样的-
curl -H "Content-Type: application/json" -d "{id=4"}" localhost:8080/api/unread
再服务
@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());
}
}
@服务
公共类服务{
私有最终RestTemplate RestTemplate;
公共RestService(RestTemplateBuilder RestTemplateBuilder){
this.restTemplate=restTemplateBuilder.build();
}
公共消息保存消息(消息消息){
字符串url=”http://localhost:8080/api/save";
返回此.restTemplate.postForObject(url、消息、消息.class);
}
公共无效更新消息(长id,消息){
字符串url=String.format(“http://localhost:8080/api/update/%d“,id);
this.restemplate.put(url、消息);
}
公共列表getLast(){
字符串url=”http://localhost:8080/api/last";
String json=restTemplate.getForObject(url,String.class);
返回new Gson().fromJson(json,new TypeToken(){}.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);
}
@org.springframework.web.bind.annotation.RestController
公共类RestController{
@自动连线
TimerTask TimerTask;
@资源
私有最终消息服务;
公共RestController(MessageService MessageService){
this.messageService=messageService;
}
@后期映射(“/api/save”)
公共响应属性保存消息(@RequestBody Message chatMessage){
returnmessageservice.add(chatMessage);
}
@GetMapping(“/api/last”)
公共字符串getLasts(){
返回新的Gson().toJson(messageService.getLast());
}
@GetMapping(“/api/未读”)
public void getUnderMessages(){
timerTask.run();
}
@GetMapping(“/api/unread/{id}”)
公共列表getUnreadById(@PathVariable(“id”)长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);
}
}
}
@服务
@交易的
公共类MessageServiceImpl实现MessageService{
私有最终消息存储库;
私人最终页面请求lastRequest;
private List chekedMessages=new ArrayList();
@自动连线
public MessageServiceImpl(MessageRepository存储库){
this.repository=存储库;
lastRequest=新页面请求(0,10,Sort.Direction.DESC,“id”);
}
@凌驾
公共响应属性添加(@RequestBody Message){
试一试{
message.setTime(新的时间戳(new Date().getTime());
保存(消息);
返回新的响应(“Сбббббббб”,HttpStatus.OK);
}捕获(例外e){
返回新的ResponseEntity(例如toString(),HttpStatus.CONFLICT);
}
}
@凌驾
公共列表getAllMessages(){
返回repository.findAll();
}
@凌驾
公共列表getLast(){
List result=repository.findAll(lastRequest.getContent();
返回result.stream()
.sorted(Comparator.comparingLong(Message::getId))
.collect(Collectors.toList());
}
@凌驾
公共列表getUnreadById(长id){
返回repository.getUnreadById(id);
}
@凌驾
公共字符串getUnderMessages(){
List out=new ArrayList();
List uncheckedMessages=repository.findAll();
对于(消息:取消勾选消息){
如果(!chekedMessages.contains(message.getId())){
添加(message.getId());
out.add(消息);
}
}
返回新的Gson().toJson(out);
}
@凌驾
公共无效更新消息(长id,消息){
if(repository.findById(id).isPresent()){
message.setId(id);
保存(消息);
}
}
}
尝试使用以下curl请求
curl -X GET --header 'Accept: application/json' 'http://localhost:8080/api/unread/4'
请以更好的方式解释您的问题。我如何才能为POST调用正确编写curl请求?我写了,但他们告诉我这是错误的。您需要编写类似于-curl-H“Content-Type:application/json”-d“{id=4”}“localhost:8080/api/unreadok现在也许我得到了你的问题需要发送Jason并让他回来。示例-curl-H”Content-Type:application/json“-d”{id=4}“localhost:8080/api/UnreadinIntegrated swagger它将为你的api生成curl请求。curl-H“Content-Type:application/json”-X POST localhost:8080/api/save{”time“:2019-12-27T09:50:02.000+0000,“未读”:true,“消息”:“asd”,“from”:“Vadim”}curl-X POST--header'Content Type:application/json'--header'Accept:application/json'-d'{\”time:“2019-12-27T09:50:02.000+0000”,“未读”:“true”,“message”:“asd”,“from”:“Vadim”\}”