Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 类型参数“S”的推断类型“S”不在其范围内;_Java_Json_Spring Boot_Mapping - Fatal编程技术网

Java 类型参数“S”的推断类型“S”不在其范围内;

Java 类型参数“S”的推断类型“S”不在其范围内;,java,json,spring-boot,mapping,Java,Json,Spring Boot,Mapping,我在Userservice.java文件中发现了这个错误 类型参数“S”的“推断类型”不在其范围内;' 我不知道我在说什么,也不知道我应该在哪里改变。。 请检查我的档案并给我一些建议 用户控制器 package com.therealdanvega.controller; import com.therealdanvega.domain.User; import com.therealdanvega.service.UserService; import org.springframewor

我在Userservice.java文件中发现了这个错误 类型参数“S”的“推断类型”不在其范围内;' 我不知道我在说什么,也不知道我应该在哪里改变。。 请检查我的档案并给我一些建议

用户控制器

package com.therealdanvega.controller;


import com.therealdanvega.domain.User;
import com.therealdanvega.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/users")
public class UserController {

  private UserService userService;

  public UserController(UserService userService) {
    this.userService = userService;
  }

  @GetMapping("/list")
  public Iterable < User > list() {
    return userService.list();
  }
}
JsondbApplication

package com.therealdanvega;

import com.fasterxml.jackson.core.type.TypeReference;
import com.therealdanvega.domain.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.therealdanvega.service.UserService;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;


import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@SpringBootApplication
public class JsondbApplication {

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

    @Bean
    CommandLineRunner runner(UserService userService){
        return args -> {
            // read JSON and load json
            ObjectMapper mapper = new ObjectMapper();
            TypeReference<List<User>> typeReference = new TypeReference<List<User>>(){};
            InputStream inputStream = TypeReference.class.getResourceAsStream("/json/users.json");
            try {
                List<User> users = mapper.readValue(inputStream,typeReference);
                userService.save(users);
                System.out.println("Users Saved!");
            } catch (IOException e){
                System.out.println("Unable to save users: " + e.getMessage());
            }
        };
    }
}
用户存储库

package com.therealdanvega.repository;

import com.therealdanvega.domain.User;
import org.springframework.data.repository.CrudRepository;


public interface UserRepository extends CrudRepository<User,Long> {

}

存储列表用户方法userRepository中的类内UserService。存储用户抛出错误类型参数“S”的推断类型“S”不在其范围内;应该扩展“com.therealdanvega.domain.User”

我遇到了类似的问题,通过使用repository.saveAll方法,它得到了解决

我遇到了类似的问题,通过使用repository.saveAll方法,问题得到了解决

您应该调用saveAll而不是save吗?您应该调用saveAll而不是save吗?
package com.therealdanvega;

import com.fasterxml.jackson.core.type.TypeReference;
import com.therealdanvega.domain.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.therealdanvega.service.UserService;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;


import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@SpringBootApplication
public class JsondbApplication {

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

    @Bean
    CommandLineRunner runner(UserService userService){
        return args -> {
            // read JSON and load json
            ObjectMapper mapper = new ObjectMapper();
            TypeReference<List<User>> typeReference = new TypeReference<List<User>>(){};
            InputStream inputStream = TypeReference.class.getResourceAsStream("/json/users.json");
            try {
                List<User> users = mapper.readValue(inputStream,typeReference);
                userService.save(users);
                System.out.println("Users Saved!");
            } catch (IOException e){
                System.out.println("Unable to save users: " + e.getMessage());
            }
        };
    }
}
package com.therealdanvega.repository;

import com.therealdanvega.domain.User;
import org.springframework.data.repository.CrudRepository;


public interface UserRepository extends CrudRepository<User,Long> {

}