java的后端请求路径有base就报404
时间: 2024-05-03 14:18:26 浏览: 6
在Java后端开发中,通常会使用一些框架来进行路由管理,比如Spring框架中的@RequestMapping注解。如果你的请求路径中含有base,而且你没有在路由管理中添加对应的路由,那么就会返回404错误。
例如,如果你的请求路径为http://localhost:8080/base/test,而你的路由管理中只有/test这个路由,那么访问http://localhost:8080/base/test就会返回404错误。
解决方法是,在路由管理中添加对应的路由。如果使用Spring框架,可以使用@RequestMapping注解来添加路由,例如:
```java
@RestController
@RequestMapping("/base")
public class TestController {
@GetMapping("/test")
public String test() {
return "Hello World";
}
}
```
这样,访问http://localhost:8080/base/test就能够正确地返回"Hello World"了。
相关问题
基于前端vue 后端java 实现前端传加密密文 后端解密的集中方法 并用代码实现
前端Vue传递加密密文到后端Java,后解密的示例代码如下:
前端Vue加密代码:
```javascript
// 导入jsencrypt库
import JSEncrypt from 'jsencrypt'
// 创建RSA加密实例
const encrypt = new JSEncrypt()
// 设置RSA公钥
const publicKey = 'YOUR_RSA_PUBLIC_KEY'
encrypt.setPublicKey(publicKey)
// 要加密的数据
const data = 'YOUR_DATA_TO_ENCRYPT'
// 使用RSA公钥进行加密
const encryptedData = encrypt.encrypt(data)
// 将加密后的数据发送到后端
// 例如使用axios发送POST请求
axios.post('/api/decrypt', { encryptedData })
.then(response => {
console.log(response.data)
})
.catch(error => {
console.error(error)
})
```
后端Java解密代码:
```java
import org.apache.commons.codec.binary.Base64;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
@RestController
public class DecryptionController {
// 将Base64编码后的私钥字符串转换为PrivateKey对象
private PrivateKey getPrivateKey(String privateKeyStr) throws Exception {
byte[] privateKeyBytes = Base64.decodeBase64(privateKeyStr);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePrivate(keySpec);
}
@PostMapping("/api/decrypt")
public String decryptData(@RequestBody EncryptedData encryptedData) throws Exception {
String privateKeyStr = "YOUR_RSA_PRIVATE_KEY";
PrivateKey privateKey = getPrivateKey(privateKeyStr);
// 使用私钥进行解密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] encryptedBytes = Base64.decodeBase64(encryptedData.getEncryptedData());
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
// 返回解密后的数据
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
}
```
创建一个名为 `EncryptedData` 的Java类,用于接收前端传递的加密数据:
```java
public class EncryptedData {
private String encryptedData;
public String getEncryptedData() {
return encryptedData;
}
public void setEncryptedData(String encryptedData) {
this.encryptedData = encryptedData;
}
}
```
请将 `YOUR_RSA_PUBLIC_KEY` 替换为你的RSA公钥,将 `YOUR_RSA_PRIVATE_KEY` 替换为你的RSA私钥。在前端,你可以使用axios或其他HTTP库发送POST请求到后端的 `/api/decrypt` 路径,并将加密后的数据作为请求体的 `encryptedData` 字段传递。后端将解密后的数据作为响应返回给前端。
uniapp微信小程序换头像,MySQL后端用Springboot
要实现 uniapp 微信小程序的头像更换功能,可以通过以下步骤来完成:
1. 在小程序中调用微信的 `chooseImage` 接口,获取用户选择的图片文件,并将其转为 base64 编码的字符串格式。
2. 将 base64 编码的字符串发送到后端 Springboot 服务器,使用 `Base64.decodeBase64` 方法将其解码为字节数组。
3. 将字节数组转为文件,保存到服务器本地的文件系统中。
4. 将文件的保存路径和用户的信息(如用户 ID)存入 MySQL 数据库中。
5. 当用户需要查看自己的头像时,从数据库中获取头像文件的路径,将其读取为字节数组,然后将其转为 base64 编码的字符串返回给小程序。
具体实现可以参考以下代码:
在 uniapp 中实现选择图片并发送到后端服务器:
```javascript
//uniapp 选择图片并转为 base64 字符串
uni.chooseImage({
count: 1,
success: function (res) {
let tempFilePaths = res.tempFilePaths;
uni.getFileSystemManager().readFile({
filePath: tempFilePaths[0],
encoding: 'base64',
success: function (result) {
let base64 = result.data;
//发送请求到后端服务器
uni.request({
url: 'http://localhost:8080/upload',
method: 'POST',
header: {
'content-type': 'application/json'
},
data: {
image: base64
},
success: function (res) {
console.log(res);
},
fail: function (res) {
console.log("上传失败");
}
})
},
fail: function (e) {
console.log(e);
}
})
}
})
```
在 Springboot 中实现接收并保存图片:
```java
@RestController
public class ImageController {
@PostMapping(value = "/upload")
public String uploadImage(@RequestBody Map<String, String> requestMap) {
String base64 = requestMap.get("image");
//将 base64 编码的字符串转为字节数组
byte[] bytes = Base64.decodeBase64(base64);
//将字节数组转为文件,保存到服务器本地的文件系统中
String filePath = "/path/to/save/image";
try {
FileOutputStream fos = new FileOutputStream(filePath);
fos.write(bytes);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
//将文件的保存路径和用户信息存入 MySQL 数据库中
String userId = "123456";
String sql = "INSERT INTO user (user_id, avatar_path) VALUES (?, ?)";
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, userId);
pstmt.setString(2, filePath);
pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
}
```
在 Springboot 中实现获取用户头像并返回 base64 字符串:
```java
@RestController
public class ImageController {
@GetMapping(value = "/avatar/{userId}")
public String getAvatar(@PathVariable("userId") String userId) {
String sql = "SELECT avatar_path FROM user WHERE user_id = ?";
String filePath = "";
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, userId);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
filePath = rs.getString("avatar_path");
}
rs.close();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
//将头像文件读取为字节数组并转为 base64 编码的字符串
String base64 = "";
try {
FileInputStream fis = new FileInputStream(filePath);
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
fis.close();
base64 = Base64.encodeBase64String(bytes);
} catch (Exception e) {
e.printStackTrace();
}
return base64;
}
}
```