- Python (使用Flask框架)jxhcwhyp.com
python
from flask import Flask, request, jsonify
app = Flask(name)
假设的游戏商品列表
games = [
{“id”: 1, “name”: “Game A”, “price”: 9.99},
{“id”: 2, “name”: “Game B”, “price”: 19.99},
# … 更多游戏
]
@app.route(‘/games’, methods=[‘GET’])
def get_games():
return jsonify(games)
@app.route(‘/games/int:game_id’, methods=[‘GET’])
def get_game(game_id):
for game in games:
if game[‘id’] == game_id:
return jsonify(game)
return jsonify({‘error’: ‘Game not found’}), 404
启动应用
if name == ‘main’:
app.run(debug=True)
2. JavaScript (使用Express框架和Node.js)
javascript
const express = require(‘express’);
const app = express();
const games = [
{id: 1, name: ‘Game A’, price: 9.99},
{id: 2, name: ‘Game B’, price: 19.99},
// … 更多游戏
];
app.get(‘/games’, (req, res) => {
res.json(games);
});
app.get(‘/games/:game_id’, (req, res) => {
const gameId = parseInt(req.params.game_id);
const game = games.find(game => game.id === gameId);
if (game) {
res.json(game);
} else {
res.status(404).json({error: ‘Game not found’});
}
});
app.listen(3000, () => console.log(‘Server started on port 3000’));
3. Java (使用Spring Boot框架)
在Spring Boot中,你需要创建多个类来定义控制器、服务、实体等。以下是一个简化的示例:
Game.java (实体类)
java
public class Game {
private Long id;
private String name;
private double price;
// … getters, setters, toString
}
GameController.java (控制器类)
java
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
@RestController
@RequestMapping(“/games”)
public class GameController {
private static List<Game> games = Arrays.asList(
new Game(1L, "Game A", 9.99),
new Game(2L, "Game B", 19.99)
// ... 更多游戏
);
@GetMapping("")
public List<Game> getGames() {
return games;
}
@GetMapping("/{gameId}")
public Game getGame(@PathVariable Long gameId) {
for (Game game : games) {
if (game.getId().equals(gameId)) {
return game;
}
}
return null; // 在实际应用中,应该返回一个错误响应
}
}
请注意,在Spring Boot应用中,你还需要配置许多其他事项,如数据源、事务管理、安全性等。这些示例只是展示了如何定义基本的路由和响应。由于不同的编程语言有其独特的特性和应用场景,下面我将用三种常见的编程语言(Python、JavaScript、Java)来简要描述一个游戏商城的核心部分代码。请注意,这些代码只是示例,并不能直接用于生产环境,因为它们缺少许多必要的特性,如数据库交互、用户认证、错误处理等。
- Python (使用Flask框架)
python
from flask import Flask, request, jsonify
app = Flask(name)
假设的游戏商品列表
games = [
{“id”: 1, “name”: “Game A”, “price”: 9.99},
{“id”: 2, “name”: “Game B”, “price”: 19.99},
# … 更多游戏
]
@app.route(‘/games’, methods=[‘GET’])
def get_games():
return jsonify(games)
@app.route(‘/games/int:game_id’, methods=[‘GET’])
def get_game(game_id):
for game in games:
if game[‘id’] == game_id:
return jsonify(game)
return jsonify({‘error’: ‘Game not found’}), 404
启动应用
if name == ‘main’:
app.run(debug=True)
2. JavaScript (使用Express框架和Node.js)
javascript
const express = require(‘express’);
const app = express();
const games = [
{id: 1, name: ‘Game A’, price: 9.99},
{id: 2, name: ‘Game B’, price: 19.99},
// … 更多游戏
];
app.get(‘/games’, (req, res) => {
res.json(games);
});
app.get(‘/games/:game_id’, (req, res) => {
const gameId = parseInt(req.params.game_id);
const game = games.find(game => game.id === gameId);
if (game) {
res.json(game);
} else {
res.status(404).json({error: ‘Game not found’});
}
});
app.listen(3000, () => console.log(‘Server started on port 3000’));
3. Java (使用Spring Boot框架)
在Spring Boot中,你需要创建多个类来定义控制器、服务、实体等。以下是一个简化的示例:
Game.java (实体类)
java
public class Game {
private Long id;
private String name;
private double price;
// … getters, setters, toString
}
GameController.java (控制器类)
java
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
@RestController
@RequestMapping(“/games”)
public class GameController {
private static List<Game> games = Arrays.asList(
new Game(1L, "Game A", 9.99),
new Game(2L, "Game B", 19.99)
// ... 更多游戏
);
@GetMapping("")
public List<Game> getGames() {
return games;
}
@GetMapping("/{gameId}")
public Game getGame(@PathVariable Long gameId) {
for (Game game : games) {
if (game.getId().equals(gameId)) {
return game;
}
}
return null; // 在实际应用中,应该返回一个错误响应
}
}
请注意,在Spring Boot应用中,你还需要配置许多其他事项,如数据源、事务管理、安全性等。这些示例只是展示了如何定义基本的路由和响应。