gpt4 book ai didi

Where should be a business logic if I use HTTP interface in my Spring Boot application?(如果我在我的Spring Boot应用程序中使用HTTP接口,哪里应该是业务逻辑?)

转载 作者:bug小助手 更新时间:2023-10-25 14:44:16 25 4
gpt4 key购买 nike



I have a task and condition: "The service should receive the data at HTTP
interface, store them in SQL database and make them available via the HTTP interface.". If I understand it correctly then in my service layer I should use @HttpExchange, @GetExchange etc. annotations. In all youtube videos that I saw all people wrote a close service without business logic and put this logic in controller part. Is i right way to use HTTP interfaces? Or it will be more correctly to put business logic in service layer?

我有一个任务和条件:“服务应该在HTTP接口接收数据,将它们存储在SQL数据库中,并通过HTTP接口使它们可用。”如果我理解正确,那么在我的服务层中,我应该使用@HttpExchange、@GetExchange等注释。在我看到的所有YouTube视频中,所有人都编写了一个没有业务逻辑的封闭服务,并将此逻辑放在控制器部分。我使用HTTP接口的方式正确吗?或者将业务逻辑放在服务层会更正确?


My Service

我的服务


@HttpExchange("/transactions")
public interface TransactionClient {
@GetExchange
List<Transaction> findAll();

@GetExchange("/{id}")
Transaction findOne(@PathVariable Integer id);

@PostExchange
ResponseEntity<HttpStatus> saveTransaction(@RequestBody Transaction transaction);

@PatchExchange("/{id}")
void updateTransaction(@RequestBody Transaction transaction, @PathVariable Integer id);

@DeleteExchange("/{id}")
void deleteTransaction(@PathVariable Integer id);
}


My Controller

我的控制器



@RestController
@Transactional(readOnly = true)
@RequestMapping("/transactions")
public class TransactionController {
private final TransactionRepository transactionRepository;


@Autowired
public TransactionController(TransactionRepository transactionRepository) {
this.transactionRepository = transactionRepository;
}


@GetMapping
public List<Transaction> findAll() {
return transactionRepository.findAll();
}

@GetMapping("/{id}")
public Transaction findOne(@PathVariable("id") Integer id) {
return transactionRepository.findById(id).orElse(null);
}

@Transactional
@PostMapping
public ResponseEntity<HttpStatus> saveTransaction(@RequestBody Transaction transaction) {
transaction.setDateOfTransaction(new Timestamp(System.currentTimeMillis()));
transactionRepository.save(transaction);
return ResponseEntity.ok(HttpStatus.OK);
}

@Transactional
@PatchMapping("/{id}")
public void updateTransaction(@RequestBody Transaction transaction, @PathVariable("id") Integer id) {
Transaction transactionFromDb = transactionRepository.findById(id).orElse(null);
transaction.setId(id);
transaction.setDateOfTransaction(transactionFromDb.getDateOfTransaction());
transactionRepository.save(transaction);
}

@DeleteMapping("/{id}")
@Transactional
public void deleteTransaction(@PathVariable("id") Integer id) {
transactionRepository.deleteById(id);
}
}

I watched a lot of videos on youtube but they put the logic in controller.Is it right?

我在YouTube上看了很多视频,但他们把逻辑放在了控制器上。对吗?


更多回答
优秀答案推荐

If you have a lot of business logic i would recommend to use a @Service class instead putting everything into the @Controller class. You can then inject the @Service class to the controller and calling the methodes on the @Service class.

如果您有很多业务逻辑,我建议您使用@Service类,而不是将所有内容都放入@Control类。然后,您可以将@Service类注入控制器并调用@Service类上的方法。


Controller class:

控制器类:


@RestController
@AllArgsConstructor
@RequestMapping(value = "api")
public class TestResource {
private final TestService testService;

@GetMapping(value = "test")
public ResponseEntity<String> test(
Principal principal
) {
testService.test();
return ResponseEntity.noContent().build();
}
}

Service class:

服务等级:



@Service
public class TestService {

public void test() {
// do something here …
}

}


更多回答

But when I use HTTP interface the service layer is not a class. It's an interface. And when I try to inject for example repository I can't do it with private access modifier. Is it normal if it will be public? or would it be better to leave all the logic in the controller?

但是当我使用HTTP接口时,服务层不是一个类。这是一个接口。当我尝试注入例如存储库时,我不能用私有访问修饰符来做。如果是公开的,正常吗?还是把所有的逻辑都留在控制器里更好?

I add an example, please check it. This is the normal way how to abstract the business logic from the controller class. In the service you can also implement a interface if needed to add your methodes there.

我加了一个例子,请检查一下。这是从控制器类抽象业务逻辑的正常方式。在服务中,如果需要,您还可以实现一个接口,以便在那里添加您的方法。

Thanks, but in my case the Service is interface.In your case it still class

谢谢,但在我的情况下,服务是接口的。在您的情况下,它仍然是类

Why can you not implement the interface to the class?

为什么不能实现对类的接口呢?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com