简体   繁体   中英

Unable to use java.net.http in Maven project using Spring Java 12

I am trying to use java.net.http library to use HTTP functions, however my IntelliJ compiler is not recognizing it.

The error I receive on the package side: java: error: "invalid target release: 12"

in regards to the entire project here is another error that I receive: Plugin 'org.springframework.boot:spring-boot-maven-plugin:' not found

I have made sure I properly installed Java 12 as well as set it as the project & target bytecode version. Im relatively new to coding in this environment so if there's anything I can do to better describe my question please let me know.

Here is the maven pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>io.rooseveltthedev</groupId>
    <artifactId>coronavirus-tracker</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>coronavirus-tracker</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>12</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

This is the code where I am having difficulty with the HTTP import:

import java.net.http.HttpClient;

public class CoronaVirusDataService {

    public static String VIRUS_DATA_URL = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";


    public void fetchVirusData(){
        HttpClient client = HttpClient.newHttpClient();

    }

}

also I noticed there are some lines that refuse to go into the code formatting any remarks on how to fix that for future questions would be great.

This should be some maven project / compiler and/or class-path related issue in your development environment.

I could not reproduce your error. I am successfully using java.net.http.HttpClient with Spring Boot 2.4 and Java 12 (also Java 11 LTS.). HttpClient is connecting getting back 200 status codes...

Any issues you have is not related to java.net.http libraries with Spring Boot and Java 12...

I would try to mvn compile and package using command line without the IDE. Also, do you need spring boot dev-tools runtime dependency in your pom? Try to remove it...

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