@Autowired获取空值

时间:2019-02-18 12:07:48

标签: java spring spring-boot

在Login类中,@ Autowired不为null,但是当我尝试使用邮递员呼叫时,注销类@Autowired获得null。我将代码从Login复制到Logout类,只是更改变量名,所有代码都是同一模式,我不知道发生了什么

package xxx.api.login
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.databind.ObjectMapper;
import xxx.api.domain.logout.LoginRequest;
import xxx.api.domain.logout.LoginResponse;
import xxx.converter.FieldValidation;
import xxx.dto.log.MessageLog;
import xxx.logging.LogFactory;

@Component
public class LoginClient {

    @Autowired
    FieldValidation fieldValidation;

    public LoginResponse login(LoginRequest loginRequest) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        final String url = "http://localhost:xxx/xxx/login";
        LoginResponse responseObject = null;
        try {
            String requestData = mapper.writeValueAsString(loginRequest);
            responseObject = login(url, requestData);
            fieldValidation.validateResponse(responseObject, "login"); 
// 'fieldValidation' not null

这是我的登录课程。按预期返回结果。

package xxx.api.logout;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.databind.ObjectMapper;
import xxx.api.domain.logout.LogoutRequest;
import xxx.api.domain.logout.LogoutResponse;
import xxx.converter.FieldValidation;
import xxx.dto.log.MessageLog;
import xxx.logging.LogFactory;

@Component
public class LogoutClient {

    @Autowired
    FieldValidation fieldValidation;

    public LogoutResponse logout(LogoutRequest logoutRequest) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        final String url = "http://localhost:xxx/xxx/logout";
        LogoutResponse responseObject = null;
        try {
            String requestData = mapper.writeValueAsString(logoutRequest);
            responseObject = logout(url, requestData);
            fieldValidation.validateResponse(responseObject, "logout"); 
// 'fieldValidation' is null then throws nullPointerException

这是我的注销课程

package xxx.api.logout;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import xxx.api.domain.logout.LogoutRequest;
import xxx.api.domain.logout.LogoutResponse;
import xxx.dto.log.MessageLog;
import xxx.exception.common.ErrorException;
import xxx.api.logout.LogoutClient;
import xxx.converter.FieldValidation;

@RestController
@RequestMapping(path = "/xxx")
public class LogoutServer {

    @Autowired
    FieldValidation fieldValidation;

    @Autowired
    WSLogFactory wsLog;

    @PostMapping(value = "/logout", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<?> logout(@RequestHeader(value = "requestId") String requestId,
            @RequestBody(required = true) LogoutRequest logoutRequest) throws ErrorException, Exception {
        writeLogRequest(logoutRequest, logoutRequest.getClientIP(), "Request", "/xxx/logout");
        fieldValidation.validateRequest(logoutRequest, "logout");
        try {
            LogoutClient logoutClient = new LogoutClient();
            LogoutResponse response = logoutClient.logout(logoutRequest);
            return new ResponseEntity<LogoutResponse>(response, HttpStatus.OK);

        } catch (Exception e) {
            throw e;
        }
    }

这是注销控制器

@org.springframework.context.annotation.Configuration
@PropertySource("file:${layout.properties_file}")
public class FieldValidation {
      // do somethings
}

这是我的FieldValidation类。

{
    "timestamp": 1550490230074,
    "status": 500,
    "error": "Internal Server Error",
    "exception": "java.lang.NullPointerException",
    "message": "No message available",
    "path": "/umm/logout"
}

这是我调用注销类时的返回。

这是我的项目结构

xxx.api.login <<< LoginClient.java
xxx.api.logout <<< LogoutClient.java
xxx.converter <<< FieldValidation.java

这是我的主程序

package xxx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

我尝试将@Component替换为@Configuration,但仍然无法正常工作。

我不需要Logout类中的'fieldValidation'或另一个类为空值。

package xxx.converter;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Year;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.format.ResolverStyle;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import org.springframework.context.annotation.Configuration;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.validator.routines.BigDecimalValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

import com.fasterxml.jackson.databind.ObjectMapper;

import xxx.exception.common.ErrorException;

@Configuration
@PropertySource("file:${layout.properties_file}")
public class FieldValidation {
public void validateObject(Object objResponse, String config) throws Exception {
    try {
        List<Configuration> confs = objectMapper.readValue(config,
                objectMapper.getTypeFactory().constructCollectionType(List.class, Configuration.class));
        for (int i = 0; i < confs.size(); i++) {
            Configuration configuration = confs.get(i);
            Object objValue = getValue(configuration.getFieldName(), objResponse);

            Validation validation = new Validation();
            BeanUtils.copyProperties(validation, configuration);
            isValid(objValue, validation);

            if (configuration.getType().equalsIgnoreCase("object")) {
                List<Validation> validations = configuration.getValidation();
                Class<?> act = Class.forName(configuration.getClassName());
                Object objectConfig = act.cast(objValue);
                validateObject(objectConfig, validations);

            }

        }

    } catch (Exception e) {
        throw e;
    }
}

这是FieldValidation类的完整导入,并替换了@Configuration,我收到此错误。

configuration.getFieldName()<<错误类型Configuration的方法未定义

configuration.getType()<<错误类型配置未定义方法

configuration.getValidation()<<错误类型Configuration的方法未定义

configuration.getClassName()<<错误类型Configuration的方法未定义

0 个答案:

没有答案