春季启动:应用程序无法启动

时间:2020-04-21 20:10:07

标签: java spring-boot

Dear All,

I m new to Spring-Boot. I have created this service restful service, which generates the following error. I understand that NativeWebRequest is an Interface and needs to be implemented by a bean that will implement this interface, but I don't know if this is the right approach? 
Unfortunately, I spend already many days trying to understand what could be the origin of the problem. Any help/advice will really be appreciated.

Thank you for your attention.

Evangelos
The Application

...

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) The Interface

...

@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) The Code Implementation

...

@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);
    }

}

...

The Service definition.

...

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) Final Error

...

***************************
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

...

1 个答案:

答案 0 :(得分:0)

我认为错误是这样的:

{{1}}

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

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