vue和Springboot怎么进行通信
时间: 2024-03-07 07:51:57 AIGC 浏览: 22
Vue和Spring Boot可以通过RESTful API进行通信。
在Spring Boot中,您可以使用Spring MVC构建RESTful API。您可以创建一个控制器类,并在其中定义处理不同HTTP请求方法的端点。例如:
```java
@RestController
@RequestMapping("/api")
public class MyController {
@GetMapping("/hello")
public String hello() {
return "Hello, world!";
}
@PostMapping("/create")
public ResponseEntity<Object> create(@RequestBody MyObject myObject) {
// create object logic
return ResponseEntity.ok().build();
}
}
```
在Vue中,您可以使用Axios库进行HTTP请求。例如:
```javascript
axios.get('http://localhost:8080/api/hello')
.then(response => console.log(response.data))
.catch(error => console.error(error));
axios.post('http://localhost:8080/api/create', { name: 'My Object' })
.then(response => console.log(response.status))
.catch(error => console.error(error));
```
在上面的代码中,我们使用Axios库发送GET和POST请求到Spring Boot应用程序的RESTful API。注意,我们使用的URL是Spring Boot应用程序的URL,端口号为8080,这个URL应该根据你的实际情况进行更改。
阅读全文
相关推荐


















