首页 > 解决方案 > Spring-boot:应用程序无法启动

问题描述

各位,

我是 Spring-Boot 的新手。我已经创建了这个服务 restful 服务,它会产生以下错误。我知道 NativeWebRequest 是一个接口,需要由一个实现这个接口的 bean 来实现,但我不知道这是否是正确的方法?
不幸的是,我已经花了很多天试图了解问题的根源。任何帮助/建议将不胜感激。

感谢您的关注。

伊万杰洛斯
应用程序

...

package server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import server.service.controllers.CSVReadAndParseToBean;


@SpringBootApplication
@Configuration
public class Application {

    public static void InitializeReferenceData() throws Exception {

        CSVReadAndParseToBean csv2bean= new CSVReadAndParseToBean();
        csv2bean.InitializeRefEicCode();

    }

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

...

2) 接口

...

@Validated
@Api(value = "execute_rules", description = "Execute the rule validation on the received message")
public interface ExecuteRulesApi {

    default Optional<NativeWebRequest> getRequest() {
        return Optional.empty();
    }

    @ApiOperation(value = "", nickname = "executeRulesPost", notes = "", response = ResultMsg.class, tags={  })
    @ApiResponses(value = { 
        @ApiResponse(code = 201, message = "Return an object with the validation status ", response = ResultMsg.class) })
    @RequestMapping(value = "/execute_rules",
        produces = { "application/json" }, 
        consumes = { "application/json" },
        method = RequestMethod.POST)
    default ResponseEntity<ResultMsg> executeRulesPost(@ApiParam(value = "" ,required=true )  @Valid @RequestBody RequestMsg requestMsg) throws JsonProcessingException {
        System.out.println (" **** Call to - ExecuteRulesApiInterface");
         String result = ApiUtil.processMessageBody(requestMsg);
        getRequest().ifPresent(request -> {
            for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                    ApiUtil.setMsgResponse(request, "application/json", result);
                    break;
                }
            }
            System.out.println (" **** Call to Exit ExecuteRulesApiInterface");
        });    ***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in server.service.api.ExecuteRulesApiController required a bean of type 'org.springframework.web.context.request.NativeWebRequest'
 that could not be found.


Action:

Consider defining a bean of type 'org.springframework.web.context.request.NativeWebRequest' in your configuration.


Process finished with exit code 1    ***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in server.service.api.ExecuteRulesApiController required a bean of type 'org.springframework.web.context.request.NativeWebRequest'
 that could not be found.


Action:

Consider defining a bean of type 'org.springframework.web.context.request.NativeWebRequest' in your configuration.


Process finished with exit code 1

        return new ResponseEntity<>(HttpStatus.CREATED);

    }

...

3) 代码实现

...

@Controller
@RequestMapping("${openapi.demoNewTPRulesEngine.base-path:/api-rules}")

public class ExecuteRulesApiController implements ExecuteRulesApi {

    private @Autowired
    final NativeWebRequest request;

    @org.springframework.beans.factory.annotation.Autowired
    public ExecuteRulesApiController(NativeWebRequest request) {
        this.request = request;
        System.out.println (" **** Call to Constructor of ExecuteRulesApiController *****");

    }

    @Override

    public Optional<NativeWebRequest> getRequest( ) {

        System.out.println (" **** Call to Override getRequest methodes.*****");
        System.out.println (" **** Received Payload is:" +this.request.toString());

        return Optional.ofNullable(request);
    }

}

...

服务定义。

...

4) Beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean class="org.springframework.web.context.request.NativeWebRequest" abstract="true"/>
        <bean/>

</beans>

...

5) 最终错误

...

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in server.service.api.ExecuteRulesApiController required a bean of type 'org.springframework.web.context.request.NativeWebRequest'
 that could not be found.


Action:

Consider defining a bean of type 'org.springframework.web.context.request.NativeWebRequest' in your configuration.


Process finished with exit code 1

...

标签: javaspring-boot

解决方案


我认为错误是这个:

<bean class="org.springframework.web.context.request.NativeWebRequest" abstract="true"/>

如果将其声明为抽象,则 Spring 不会创建实例,spring 配置中的“抽象”bean 用于设置为其他 bean 的父类。

NativeWebRequest 是一个接口,你可以创建一个接口的 bean,尝试其中一个实现。也许是 ServletWebRequest?


推荐阅读