【发布时间】:2019-01-30 18:25:54
【问题描述】:
我有一个带有 Autowired 组件和方法的类。如果我直接调用该方法,它工作正常。但是,如果对该方法的调用来自另一个类,那么在使用 Autowired 组件的行中会出现 java.lang.NullPointerException 错误。 Autowired 组件是一个充当代理的接口组件。我已经为接口组件和自动装配组件尝试了不同的注释,但仍然出现错误。
如果直接调用该方法,我不明白为什么 Autowired 组件不为空,但如果从另一个类调用它则为空。
这是界面组件
@FeignClient(name = "authentication-server", url = "localhost:8010")
public interface AuthenticationProxy {
@GetMapping("/headers")
public HttpEntity<String> retrieveHeaders();
@GetMapping("/auth-token")
public AuthorizationTokenBean retrieveToken();
这是使用 Autowired 组件的类
@RestController
public class UserController {
@Autowired
private AuthenticationProxy authenticationProxy;
@PostMapping("/user/create")
public UserResponseBean createUser(ValuesBean userValues) {
UserCreateRequestBean bodyBean = new UserCreateRequestBean();
ValuesBean valuesBean = new ValuesBean();
bodyBean.setValues(userValues);
// This line triggers the null pointer error
// (only if method called from another class)
String token = authenticationProxy
.retrieveToken()
.getAuthorizationToken();
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", token);
headers.add("Content-type", "application/json");
headers.add("accept", "application/json");
HttpEntity<Object> requestEntity =
new HttpEntity<>(bodyBean, headers);
ResponseEntity<String> responseEntity = new RestTemplate().exchange(
"https://api.acme.com/user/create",
HttpMethod.POST,
requestEntity,
String.class
);
String output = responseEntity.getBody();
Gson gson = new Gson();
return gson.fromJson(output,UserResponseBean.class);
}
}
这是调用方法所在的类
@RestController
public class TestController {
@GetMapping("/test/user/create")
public void testUserCreate() {
ValuesBean valuesBean = new ValuesBean();
valuesBean.setDate_of_birth("1917-05-16");
valuesBean.setFirst_name("Juan");
valuesBean.setLast_name("Rulfo");
valuesBean.setGender("Male");
valuesBean.setOccupation("Escritor");
UserController testUser = new USerController();
testUSer.createUser(valuesBean);
}
}
【问题讨论】:
-
您是否将@EnableFeignClients 添加到您的应用程序类中?
-
是:@EnableFeignClients("com.acme.apimiddleware.acmetoy") public class UsersApplication { public static void main(String[] args) { SpringApplication.run(UsersApplication.class, args); } }