【问题标题】:React js not rendering through JSON from rest apiReact js 不通过来自 rest api 的 JSON 渲染
【发布时间】:2019-05-03 11:15:54
【问题描述】:

我一直在尝试将我的 springboot 应用程序与其他服务后端连接到 ReactJS 前端。连接似乎有效,但我无法通过我的 React 应用程序呈现结果。

下面是我的 app.js 文件

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';

class App extends Component {
  state = {
    isLoading: true,
    groups: []
  };

  async componentDidMount() {
    const response = await fetch('api/blog');
    const body = await response.json();
    this.setState({ groups: body, isLoading: false });
  }

  render() {
    const {groups, isLoading} = this.state;

    if (isLoading) {
      return <p>Loading...</p>;
    }

    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <div className="App-intro">
            <h2>Example</h2>
            {groups.map(group =>
              <div key={group.id}>
                {this.state.title}
              </div>
            )}
          </div>
        </header>
      </div>
    );
  }
}

export default App;

下面给出了我的休息控制器 打包我.salisuwy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/api")
@RestController
public class BlogController {

    @Autowired
    BlogRespository blogRespository;

    @GetMapping("/blog")
    public List<Blog> index(){
        return blogRespository.findAll( );
    }

    @GetMapping("/blog/{id}")
    public Blog show(@PathVariable String id){
        int blogId = Integer.parseInt(id);
        return blogRespository.findOne(blogId);
    }

    @PostMapping("/blog/search")
    public List<Blog> search(@RequestBody Map<String, String> body){
        String searchTerm = body.get("text");
        return blogRespository.findByTitleContainingOrContentContaining(searchTerm, searchTerm);
    }

    @PostMapping("/blog")
    public Blog create(@RequestBody Map<String, String> body){
        String title = body.get("title");
        String content = body.get("content");
        return blogRespository.save(new Blog(title, content));
    }

    @PutMapping("/blog/{id}")
    public Blog update(@PathVariable String id, @RequestBody Map<String, String> body){
        int blogId = Integer.parseInt(id);
        // getting blog
        Blog blog = blogRespository.findOne(blogId);
        blog.setTitle(body.get("title"));
        blog.setContent(body.get("content"));
        return blogRespository.save(blog);
    }

    @DeleteMapping("blog/{id}")
    public boolean delete(@PathVariable String id){
        int blogId = Integer.parseInt(id);
        blogRespository.delete(blogId);
        return true;
    }

}

json输出如下

0:  
id: 1
title:      "Example1"
content:    "Example Content1"
1:  
id: 2
title:      "Example1"
content:    "Example Content1"

【问题讨论】:

    标签: json spring reactjs rest


    【解决方案1】:

    您的 render 方法中有语义错误,所以不要这样:

    {groups.map(group =>
       <div key={group.id}>
         {this.state.title}
       </div>
    )}
    

    试试这个:

    {groups.map(group =>
       <div key={group.id}>
          {group.title}
       </div>
    )}
    

    【讨论】:

    • 完美运行。谢谢
    • 很高兴为您提供帮助 :) 祝您好运!
    猜你喜欢
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 2019-07-01
    • 2021-05-17
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多