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

JES*_*ANG 1 java mapping json spring-boot

我在Userservice.java文件“类型类型为'S'的推断类型为'S'的错误中没有得到此错误;” 我不知道在说什么,应该在哪里更改..请检查我的文件并给我一些建议

在此处输入图片说明

package com.therealdanvega.service;

import com.therealdanvega.domain.User;
import com.therealdanvega.repository.UserRepository;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    private UserRepository userRepository;

    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public Iterable<User> list() {
        return userRepository.findAll();
    }

    public User save(User user) {
        return userRepository.save(user);
    }

    public void save(List<User> users) {
        userRepository.save(users);
    }
}
Run Code Online (Sandbox Code Playgroud)

用户

package com.therealdanvega.domain;


import lombok.AllArgsConstructor;
import lombok.Data;

import javax.persistence.*;

@Data
@AllArgsConstructor
@Entity
public class User {

    @Id
    @GeneratedValue( strategy = GenerationType.AUTO )
    private Long id;
    private String name;
    private String username;
    private String email;
    private String phone;
    private String website;

    @Embedded
    private Address address;
    @Embedded
    private Company company;

    public User() {}
}
Run Code Online (Sandbox Code Playgroud)

用户控制器

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();
  }
}
Run Code Online (Sandbox Code Playgroud)

Jsondb应用程序

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());
            }
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

用户资料库

package com.therealdanvega.repository;

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


public interface UserRepository extends CrudRepository<User,Long> {

}
Run Code Online (Sandbox Code Playgroud)

在类UserService的save(List users)方法中,userRepository.save(users)引发错误类型参数'S'的推断类型'S'不在其范围内;应该扩展'com.therealdanvega.domain.User'

小智 6

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