随笔 - 403  文章 - 0  评论 - 6  阅读 - 3254

2024年6月4日第六十二篇

今天遇到了网络请求的问题因为我要发送一个多参数的post请求,发现不能使用@requestmapping,

于是我改为使用@PostMapping但是其中不包含参数@Requestparam只包含@RequestBody

导致问题迟迟无法解决,

最后我改为使用get请求,用@GetMapping用@RequestParam实现参数传递。

复制代码
<template>
  <div>
    <h1>文章</h1>

    <ul id="articles">
      <li v-for="article in articles" :key="article.id">
        {{ article.title }} - {{ article.content }}
        <button @click="deleteArticle(article.id)">删除</button>
        <button @click="editArticle(article)">编辑</button>
      </li>
    </ul>

    <form @submit.prevent="createArticle">
      <input type="text" v-model="newArticle.title" placeholder="标题">
      <textarea v-model="newArticle.content" placeholder="内容"></textarea>
      <button type="submit">创建</button>
    </form>

    <form v-if="editArticleId" @submit.prevent="updateArticle">
      <input type="text" v-model="editArticle.title" placeholder="标题">
      <textarea v-model="editArticle.content" placeholder="内容"></textarea>
      <button type="submit">更新</button>
    </form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      articles: [],
      newArticle: {
        title: '',
        content: ''
      },
      editArticleId: null,
      editArticle: null
    }
  },
  mounted() {
    this.getArticles();
  },
  methods: {
    getArticles() {
      axios.get('/api/articles')
        .then(response => {
          this.articles = response.data;
        })
        .catch(error => {
          console.error(error);
        });
    },
    createArticle() {
      axios.post('/api/articles', this.newArticle)
        .then(response => {
          this.articles.push(response.data);
          this.newArticle = { title: '', content: '' };
        })
        .catch(error => {
          console.error(error);
        });
    },
    deleteArticle(id) {
      axios.delete(`/api/articles/${id}`)
        .then(response => {
          this.articles = this.articles.filter(article => article.id !== id);
        })
        .catch(error => {
          console.error(error);
        });
    },
    editArticle(article) {
      this.editArticleId = article.id;
      this.editArticle = article;
    },
    updateArticle() {
      axios.put(`/api/articles/${this.editArticleId}`, this.editArticle)
        .then(response => {
          this.articles = this.articles.map(article => {
            if (article.id === this.editArticleId) {
              return response.data;
            } else {
              return article;
            }
          });
          this.editArticleId = null;
          this.editArticle = null;
        })
        .catch(error => {
          console.error(error);
        });
    }
  }
};
</script>
复制代码

 

复制代码
@RestController
@RequestMapping("/api/articles")
public class ArticleController {

  @Autowired
  private ArticleRepository articleRepository;

  @GetMapping
  public List<Article> getAllArticles() {
    return articleRepository.findAll();
  }

  @GetMapping("/{id}")
  public Article getArticleById(@PathVariable Long id) {
    return articleRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Article not found: " + id));
  }

  @PostMapping
  public Article createArticle(@RequestBody Article article) {
    return articleRepository.save(article);
  }

  @PutMapping("/{id}")
  public Article updateArticle(@PathVariable Long id, @RequestBody Article article) {
    article.setId(id);
    return articleRepository.save(article);
  }

  @DeleteMapping("/{id}")
  public void deleteArticle(@PathVariable Long id) {
复制代码

 

posted on   石铁生  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示