gpt4 book ai didi

java - Spring Boot 从服务调用 Rest Controller 方法

转载 作者:行者123 更新时间:2023-11-29 06:27:16 28 4
gpt4 key购买 nike

我正在使用 Spring Boot 从服务调用休息 Controller 方法。调用该方法时,我收到错误 java.lang.NullPointerException 广泛的情况是,我的服务从 RabbitMQ 队列接收有效负载并提取有效负载的内容,然后它应该将其传递给 Controller ​​以保存到数据库中。队列部分有效(我可以从队列中接收消息并提取内容)。数据库部分也有效。问题是从服务调用 Controller 方法。

这是我的休息 Controller

@RestController
@RequestMapping("/auth")
public class AuthController implements AuthService {
@Autowired
RabbitTemplate rabbitTemplate;

@Autowired
AuthRepository authRepository;


public AuthModel addAuthenticatable(AuthModel auth){
auth.setCreatedAt(DateTimeUtility.getDateTime());
return authRepository.save(auth);
}
}

我的服务代码:

public class QueueListener extends AuthController implements MessageListener{
private String identifier;
private JSONArray globalObject;
private int userId;
private String pin;

@Autowired
AuthController authController;

@Override
public void onMessage(Message message) {
String msg = new String(message.getBody());
String output = msg.replaceAll("\\\\", "");
String jsonified = output.substring(1, output.length()-1);

JSONArray obj = new JSONArray(jsonified);
this.globalObject = obj;
this.identifier = obj.getJSONObject(0).getString("identifier");
resolveMessage();
}
public void resolveMessage() {
if(identifier.equalsIgnoreCase("ADD_TO_AUTH")) {
for(int i = 0; i < globalObject.length(); i++){
JSONObject o = globalObject.getJSONObject(i);
this.userId = Integer.parseInt(o.getString("userId"));
this.pin = o.getString("pin");
}

AuthModel authModel = new AuthModel();
authModel.setUserId(userId);
authModel.setPin(pin);

authController.addAuthenticatable(authModel);
}
}
}

当我调用 AuthController 中的 addAuthenticatable() 方法时发生错误。任何帮助将不胜感激。

最佳答案

我希望这不会超出主题,但通常我们想要实现的是一种洋葱架构。依赖关系应该有一个方向。

您的 Controller 是您应用程序的集成点。您希望每个 REST 触发执行某些逻辑。您的 Controller 不应扩展类或实现与业务逻辑有关的接口(interface)。这部分属于另一层。

一切与逻辑有关的东西都属于服务:

@Service
public class AuthService {
@Autowired
private AuthRepository authRepository;

private String attribute;

public boolean isAuthenticated(String username) {
authRepository.doSomething();
//implementation of the logic to check if a user is authenticated.
}

public boolean authenticate(String username, char[] password) {
// implementation of logic to authenticate.
authRepository.authenticate();
}

public AuthModel save(AuthModel model) {
//implementation of saving the model
}

}

在服务层提取您的逻辑,使事情可重用。现在您可以将服务注入(inject) controller

@RestController
@RequestMapping("/auth")
public class AuthController {

@Autowired
private AuthService authService;

public AuthModel addAuthenticatable(AuthModel auth){
//process input etc..
return authService.save(auth);
}
}

或在 amqListener

@Component
public class QueueListener implements MessageListener {
@Autowired
private AuthService authService;

@Autowired
private SomeOtherService otherService;

@Override
public void onMessage(Message message) {
JSONArray array = processInput();

JSONArray obj = new JSONArray(jsonified);
String identifier = obj.getJSONObject(0).getString("identifier");
// extract the business logic to the service layer. Don't mix layer responsibilities
otherService.doYourThing(obj, identifier);

resolveMessage();
}

private JSONArray processInput(Message message) {
String msg = new String(message.getBody());
String output = msg.replaceAll("\\\\", "");
String jsonified = output.substring(1, output.length()-1);


}

和你的配置,这样你就可以让 spring 知道在哪里寻找带注释的类。

@Configuration
@ComponentScan({"your.service.packages"})
@EntityScan(basePackages = "your.model.package")
@EnableJpaRepositories("your.repository.packages")
@EnableRabbit // probaby
@EnableWebMvc // probably
public class Config {
//you could also define other beans here
@Bean
public SomeBean someBean() {
return new SomeBean();
}
}

@pvpkiran 给出了您实际问题的答案。但我希望这对你有帮助

关于java - Spring Boot 从服务调用 Rest Controller 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51781348/

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