|
|
package com.training.api;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.training.api.dto.CreateCustomerRequest;
|
|
|
import com.training.model.Customer;
|
|
|
import com.training.service.CustomerService;
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
import javax.servlet.annotation.WebServlet;
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.IOException;
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
@WebServlet("/api/customers/*") // 关键修改:使用通配符*来匹配路径参数
|
|
|
public class CustomerController extends HttpServlet {
|
|
|
private final ObjectMapper objectMapper;
|
|
|
private final CustomerService customerService;
|
|
|
|
|
|
public CustomerController() {
|
|
|
this.objectMapper = new ObjectMapper();
|
|
|
this.customerService = new CustomerService();
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
|
|
|
throws ServletException, IOException {
|
|
|
try {
|
|
|
resp.setContentType("application/json");
|
|
|
resp.setCharacterEncoding("UTF-8");
|
|
|
|
|
|
String pathInfo = req.getPathInfo(); // 获取 /api/customers 之后的路径部分,例如 "/1"
|
|
|
|
|
|
if (pathInfo == null || pathInfo.equals("/")) {
|
|
|
// 获取所有客户
|
|
|
List<Customer> customers = customerService.findAll();
|
|
|
objectMapper.writeValue(resp.getOutputStream(), customers);
|
|
|
} else {
|
|
|
// 尝试根据ID获取单个客户
|
|
|
try {
|
|
|
long id = Long.parseLong(pathInfo.substring(1)); // 去掉开头的 '/'
|
|
|
Customer customer = customerService.findById(id);
|
|
|
if (customer != null) {
|
|
|
objectMapper.writeValue(resp.getOutputStream(), customer);
|
|
|
} else {
|
|
|
resp.setStatus(HttpServletResponse.SC_NOT_FOUND); // 404 Not Found
|
|
|
resp.getWriter().write("{\"error\": \"客户不存在\"}");
|
|
|
}
|
|
|
} catch (NumberFormatException e) {
|
|
|
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST); // 400 Bad Request
|
|
|
resp.getWriter().write("{\"error\": \"无效的客户ID格式\"}");
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
|
|
e.printStackTrace();
|
|
|
resp.getWriter().write("{\"error\": \"获取客户信息失败: " + e.getMessage() + "\"}");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
|
|
|
throws ServletException, IOException {
|
|
|
try {
|
|
|
resp.setContentType("application/json");
|
|
|
resp.setCharacterEncoding("UTF-8");
|
|
|
|
|
|
CreateCustomerRequest createCustomerRequest = objectMapper.readValue(req.getInputStream(), CreateCustomerRequest.class);
|
|
|
|
|
|
Customer customer = customerService.create(createCustomerRequest);
|
|
|
|
|
|
resp.setStatus(HttpServletResponse.SC_CREATED); // 201 Created
|
|
|
objectMapper.writeValue(resp.getOutputStream(), customer);
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
|
|
e.printStackTrace();
|
|
|
resp.getWriter().write("{\"error\": \"创建客户失败: " + e.getMessage() + "\"}");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
protected void doPut(HttpServletRequest req, HttpServletResponse resp)
|
|
|
throws ServletException, IOException {
|
|
|
try {
|
|
|
resp.setContentType("application/json");
|
|
|
resp.setCharacterEncoding("UTF-8");
|
|
|
|
|
|
String pathInfo = req.getPathInfo();
|
|
|
// 检查路径是否包含ID,例如 "/1"
|
|
|
if (pathInfo == null || !pathInfo.matches("/\\d+")) {
|
|
|
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
|
|
|
resp.getWriter().write("{\"error\": \"请求URL必须包含有效的客户ID,例如 /api/customers/1\"}");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
long customerId = Long.parseLong(pathInfo.substring(1)); // 解析ID
|
|
|
CreateCustomerRequest updateRequest = objectMapper.readValue(req.getInputStream(), CreateCustomerRequest.class);
|
|
|
|
|
|
Customer updatedCustomer = customerService.update(customerId, updateRequest);
|
|
|
|
|
|
if (updatedCustomer != null) {
|
|
|
objectMapper.writeValue(resp.getOutputStream(), updatedCustomer);
|
|
|
} else {
|
|
|
resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
|
|
resp.getWriter().write("{\"error\": \"要更新的客户不存在\"}");
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
|
|
e.printStackTrace();
|
|
|
resp.getWriter().write("{\"error\": \"更新客户失败: " + e.getMessage() + "\"}");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
|
|
|
throws ServletException, IOException {
|
|
|
try {
|
|
|
resp.setContentType("application/json");
|
|
|
resp.setCharacterEncoding("UTF-8");
|
|
|
|
|
|
String pathInfo = req.getPathInfo();
|
|
|
if (pathInfo == null || !pathInfo.matches("/\\d+")) {
|
|
|
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
|
|
|
resp.getWriter().write("{\"error\": \"请求URL必须包含有效的客户ID,例如 /api/customers/1\"}");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
long customerId = Long.parseLong(pathInfo.substring(1));
|
|
|
boolean deleted = customerService.deleteById(customerId);
|
|
|
|
|
|
if (deleted) {
|
|
|
resp.setStatus(HttpServletResponse.SC_NO_CONTENT); // 204 No Content
|
|
|
} else {
|
|
|
resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
|
|
resp.getWriter().write("{\"error\": \"要删除的客户不存在\"}");
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
|
|
e.printStackTrace();
|
|
|
resp.getWriter().write("{\"error\": \"删除客户失败: " + e.getMessage() + "\"}");
|
|
|
}
|
|
|
}
|
|
|
} |