Projekt

Obecné

Profil

« Předchozí | Další » 

Revize ff054af2

Přidáno uživatelem Petr Urban před asi 2 roky(ů)

new: #10050 Vytvořit ReactJS strukturu; created dummy controller and ReactJS module. Tested controller call

Zobrazit rozdíly:

src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/config/WebConfig.java
1
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.config;
2

  
3
import org.springframework.context.annotation.Bean;
4
import org.springframework.stereotype.Component;
5
import org.springframework.web.servlet.config.annotation.CorsRegistry;
6
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
7

  
8
@Component
9
public class WebConfig {
10

  
11
    @Bean
12
    public WebMvcConfigurer corsConfigurer() {
13
        return new WebMvcConfigurer() {
14
            @Override
15
            public void addCorsMappings(CorsRegistry registry) {
16
                registry.addMapping("/**").allowedOrigins("*");
17
            }
18
        };
19
    }
20

  
21
}
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/MainController.java
1
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.controller;
2

  
3
import org.springframework.http.ResponseEntity;
4
import org.springframework.web.bind.annotation.GetMapping;
5
import org.springframework.web.bind.annotation.RequestMapping;
6
import org.springframework.web.bind.annotation.RestController;
7

  
8
@RestController
9
@RequestMapping("/v2")
10
public class MainController {
11

  
12
    @GetMapping(value = "/testCall", produces = "application/json")
13
    public ResponseEntity<String> TestController() {
14
        String response = "{\"response\": \"successfully called\"}";
15

  
16
        return ResponseEntity.ok().body(response);
17
    }
18

  
19
}
ui/src/App.tsx
1 1
import React from 'react';
2
import logo from './logo.svg';
3
import './App.css';
2
import { useState, useEffect } from 'react';
3
import axios from 'axios'
4

  
5
const App = () => {
6

  
7
  let [data, setData] = useState(null);
8
  let [loading, setLoading] = useState(false);
9

  
10
  useEffect(() => {
11
      setLoading(true);
12

  
13
      axios
14
          .get("http://localhost:8080/v2/testCall")
15
          .then((result) => {
16
              setData(result.data.response);
17
              setLoading(false);
18
          })
19
          .catch((error) => console.log(error));
20
  }, []);
21

  
22
    if (loading) {
23
        return <p>Loading...</p>
24
    }
25

  
4 26

  
5
function App() {
6 27
  return (
7
    <div className="App">
8
      <header className="App-header">
9
        <img src={logo} className="App-logo" alt="logo" />
10
        <p>
11
          Edit <code>src/App.tsx</code> and save to reload.
12
        </p>
13
        <a
14
          className="App-link"
15
          href="https://reactjs.org"
16
          target="_blank"
17
          rel="noopener noreferrer"
18
        >
19
          Learn React
20
        </a>
21
      </header>
22
    </div>
28
      <div className="container">{data}</div>
23 29
  );
24
}
30
};
31

  
25 32

  
26 33
export default App;

Také k dispozici: Unified diff