简体   繁体   中英

Java/Spring error: 'create()' in 'reactor.netty.http.server.HttpServer' cannot be applied to '(java.lang.String, int)'

I am doing a Java / Spring course. Here is the code:

package com.pinodev.helloServer;

import reactor.netty.http.server.HttpServer;

public class HelloServerApplication {

    public static void main(String[] args) {
        HttpServer server = HttpServer.create("localhost",8080);
    }
}

build.gradle:

plugins {
    id 'org.springframework.boot' version '2.4.2'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.pinodev'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'io.projectreactor:reactor-test'
}

test {
    useJUnitPlatform()
}

This is where the error occurs

error: 'create()' in 'reactor.netty.http.server.HttpServer' cannot be applied to '(java.lang.String, int)'

在此处输入图像描述

You should create the server as follows:

 HttpServer.create()
           .host("0.0.0.0")
           .port(8080)
           .handle((req, res) -> res.sendString(Flux.just("hello")))
           .bind()
           .block();

The method create() is the only one available in HttpServer . The create with two parameters must have existed in a previous version.

class HttpServer doesn't have parameters in create() method, for setting it you should use builder method for example:

 HttpServer.create()
       .host("127.0.0.1")
       .port(8080)
       .handle((req, res) -> res.sendString(Flux.just("hello")))
       .bind()
       .block();

see more here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM