- Python (使用Flask框架)qianqutech.com
python
from flask import Flask, request, jsonify
app = Flask(name)
模拟的商品数据
products = [
{“id”: 1, “name”: “Apple”, “price”: 100},
{“id”: 2, “name”: “Banana”, “price”: 50},
]
@app.route(‘/products’, methods=[‘GET’])
def get_products():
return jsonify(products)
@app.route(‘/cart’, methods=[‘POST’])
def add_to_cart():
product_id = request.json.get(‘product_id’)
# 这里只是模拟,实际中需要处理数据库和购物车逻辑
# …
return jsonify({“message”: f"Product {product_id} added to cart."})
if name == ‘main’:
app.run(debug=True)
2. JavaScript (使用Node.js和Express框架)
javascript
const express = require(‘express’);
const app = express();
const bodyParser = require(‘body-parser’);
app.use(bodyParser.json());
// 模拟的商品数据
const products = [
{id: 1, name: ‘Apple’, price: 100},
{id: 2, name: ‘Banana’, price: 50},
];
app.get(‘/products’, (req, res) => {
res.json(products);
});
app.post(‘/cart’, (req, res) => {
const productId = req.body.product_id;
// 这里只是模拟,实际中需要处理数据库和购物车逻辑
// …
res.json({message: Product ${productId} added to cart.
});
});
app.listen(3000, () => console.log(‘Server started on port 3000’));
3. Java (使用Spring Boot)
由于Java和Spring Boot的代码较长且复杂,我将只提供一个非常简化的控制器示例:
java
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping(“/api”)
public class ShoppingController {
// 模拟的商品数据
private List<Map<String, Object>> products = Arrays.asList(
Map.of("id", 1, "name", "Apple", "price", 100),
Map.of("id", 2, "name", "Banana", "price", 50)
);
@GetMapping("/products")
public List<Map<String, Object>> getProducts() {
return products;
}
@PostMapping("/cart")
public Map<String, String> addToCart(@RequestBody Map<String, Integer> requestBody) {
int productId = requestBody.get("product_id");
// 这里只是模拟,实际中需要处理数据库和购物车逻辑
// ...
return Map.of("message", "Product " + productId + " added to cart.");
}
}
请注意,这些示例仅用于演示目的,并不包含完整的购物商城功能(如用户认证、库存管理、支付处理等)。在实际开发中,你需要根据具体需求来扩展和完善这些代码。由于生成一个完整的购物商城代码超出了简单回复的范围,我将为你提供一个非常简化的版本,用几种不同的编程语言来描述其核心功能。
- Python (使用Flask框架)
python
from flask import Flask, request, jsonify
app = Flask(name)
模拟的商品数据
products = [
{“id”: 1, “name”: “Apple”, “price”: 100},
{“id”: 2, “name”: “Banana”, “price”: 50},
]
@app.route(‘/products’, methods=[‘GET’])
def get_products():
return jsonify(products)
@app.route(‘/cart’, methods=[‘POST’])
def add_to_cart():
product_id = request.json.get(‘product_id’)
# 这里只是模拟,实际中需要处理数据库和购物车逻辑
# …
return jsonify({“message”: f"Product {product_id} added to cart."})
if name == ‘main’:
app.run(debug=True)
2. JavaScript (使用Node.js和Express框架)
javascript
const express = require(‘express’);
const app = express();
const bodyParser = require(‘body-parser’);
app.use(bodyParser.json());
// 模拟的商品数据
const products = [
{id: 1, name: ‘Apple’, price: 100},
{id: 2, name: ‘Banana’, price: 50},
];
app.get(‘/products’, (req, res) => {
res.json(products);
});
app.post(‘/cart’, (req, res) => {
const productId = req.body.product_id;
// 这里只是模拟,实际中需要处理数据库和购物车逻辑
// …
res.json({message: Product ${productId} added to cart.
});
});
app.listen(3000, () => console.log(‘Server started on port 3000’));
3. Java (使用Spring Boot)
由于Java和Spring Boot的代码较长且复杂,我将只提供一个非常简化的控制器示例:
java
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping(“/api”)
public class ShoppingController {
// 模拟的商品数据
private List<Map<String, Object>> products = Arrays.asList(
Map.of("id", 1, "name", "Apple", "price", 100),
Map.of("id", 2, "name", "Banana", "price", 50)
);
@GetMapping("/products")
public List<Map<String, Object>> getProducts() {
return products;
}
@PostMapping("/cart")
public Map<String, String> addToCart(@RequestBody Map<String, Integer> requestBody) {
int productId = requestBody.get("product_id");
// 这里只是模拟,实际中需要处理数据库和购物车逻辑
// ...
return Map.of("message", "Product " + productId + " added to cart.");
}
}
请注意,这些示例仅用于演示目的,并不包含完整的购物商城功能(如用户认证、库存管理、支付处理等)。在实际开发中,你需要根据具体需求来扩展和完善这些代码。