Java 创建web服务配置的Spring引导CRUD操作
我不熟悉使用弹簧靴。我正在尝试为CRUD操作创建一个restful web服务 我已经创建了模型、存储库和以下文件: 服务文件:Java 创建web服务配置的Spring引导CRUD操作,java,spring,rest,spring-boot,web-services,Java,Spring,Rest,Spring Boot,Web Services,我不熟悉使用弹簧靴。我正在尝试为CRUD操作创建一个restful web服务 我已经创建了模型、存储库和以下文件: 服务文件: 导入org.springframework.boot.SpringApplication; 导入org.springframework.boot.autoconfigure.springboot应用程序; @SpringBoot应用程序 公共类EmployeeServiceApplication{ 公共静态void main(字符串[]args){ run(Emplo
导入org.springframework.boot.SpringApplication;
导入org.springframework.boot.autoconfigure.springboot应用程序;
@SpringBoot应用程序
公共类EmployeeServiceApplication{
公共静态void main(字符串[]args){
run(EmployeeServiceApplication.class,args);
}
}
控制器文件:
@RestController
public class Controller {
@Autowired
private EmployeeServiceDesc employeeService;
@GetMapping("/employee/")
public List<Employee> getAllEmployees() {
return employeeService.getAllEmployees();
}
@GetMapping("/employee/{employeeId}")
public Employee getEmployeeById(@PathVariable int employeeId) {
return employeeService.getEmployeeById(employeeId);
}
@PostMapping("/employee/")
public ResponseEntity<Void> add(@RequestBody Employee newEmployee, UriComponentsBuilder builder) {
Employee employee = employeeService.addEmployee(newEmployee);
if(employee == null) {
return ResponseEntity.noContent().build();
}
HttpHeaders headers = new HttpHeaders();
headers.setLocation(builder.path("/employee/{id}").buildAndExpand(employee.getId()).toUri());
return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
}
@PutMapping("/employee/")
public ResponseEntity<Employee> updateEmployee(@RequestBody Employee v) {
Employee employee = employeeService.getEmployeeById(v.getId());
if(employee == null) {
return new ResponseEntity<Employee>(HttpStatus.NOT_FOUND);
}
employee.setName(employee.getName());
employee.setDOB(employee.getDOB());
employee.setSalary(employee.getSalary());
employeeService.updateEmployee(employee);
return new ResponseEntity<Employee>(employee, HttpStatus.OK);
}
@DeleteMapping("/employee/{id}")
public ResponseEntity<Employee> deleteEmployee(@PathVariable int id) {
Employee employee = employeeService.getEmployeeById(id);
if(employee == null) {
return new ResponseEntity<Employee>(HttpStatus.FOUND);
}
employeeService.deleteEmployee(id);
return new ResponseEntity<Employee>(HttpStatus.NO_CONTENT);
}
}
@RestController
公共类控制器{
@自动连线
私人雇员服务描述雇员服务;
@GetMapping(“/employee/”)
公共列表getAllEmployees(){
return employeeService.getAllEmployees();
}
@GetMapping(“/employee/{employeeId}”)
公共雇员getEmployeeById(@PathVariable int-employeeId){
返回employeeService.getEmployeeById(employeeId);
}
@邮戳(“/employee/”)
公共响应属性添加(@RequestBody Employee newEmployee,UriComponentsBuilder builder){
Employee Employee=employeeService.addEmployee(newEmployee);
if(employee==null){
返回ResponseEntity.noContent().build();
}
HttpHeaders=新的HttpHeaders();
headers.setLocation(builder.path(“/employee/{id}”).buildAndExpand(employee.getId()).toUri());
返回新的ResponseEntity(标题,HttpStatus.CREATED);
}
@PutMapping(“/employee/”)
公共响应更新员工(@RequestBody Employee v){
Employee=employeeService.getEmployeeById(v.getId());
if(employee==null){
返回新的ResponseEntity(未找到HttpStatus.NOT_);
}
employee.setName(employee.getName());
employee.setDOB(employee.getDOB());
employee.setSalary(employee.getSalary());
employeeService.updateEmployee(员工);
返回新的响应属性(employee,HttpStatus.OK);
}
@删除映射(“/employee/{id}”)
公共响应属性deleteEmployee(@PathVariable int-id){
Employee=employeeService.getEmployeeById(id);
if(employee==null){
返回新的响应状态(HttpStatus.FOUND);
}
employeeService.deleteEmployee(id);
返回新的响应属性(HttpStatus.NO_内容);
}
}
当我通过邮递员发送请求时,我收到错误:找不到
我想我遗漏了一些配置,但不确定我应该做哪一个?有人能帮我吗?你应该有下面这样的东西(你应该映射请求) 事后评论不起作用 SpringBoot将扫描com.x.x下包中的组件,因此如果您的控制器位于com.x.x中,则需要显式扫描它
@SpringBootApplication
@ComponentScan(basePackageClasses = Controller.class) // you should be able to justified as your packages structure
public class EmployeeServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EmployeeServiceApplication.class, args);
}
}
您不一定需要@RequestMapping来公开您的服务。我相信这里的问题是因为映射末尾的“/”。试着替换
@GetMapping("/employee/")
与
你在邮递员考试中的要求终点是什么?显示邮递员请求的详细错误。您在日志中是否有任何错误/异常?请共享邮递员请求/响应的屏幕截图。这里的一切看起来都很好-您需要提供一个您在邮递员中尝试的失败端点示例。您能试一下我在注释后更新的代码吗
@SpringBootApplication
@ComponentScan(basePackageClasses = Controller.class) // you should be able to justified as your packages structure
public class EmployeeServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EmployeeServiceApplication.class, args);
}
}
@GetMapping("/employee/")
@GetMapping("/employee")