在Java 8

时间:2016-06-30 21:03:58

标签: java spring-mvc java-8

我正在开发一个Spring MVC项目,我得到一个ArrayOutOfBound异常。如果这是Java 7那么我没有问题,但由于它是Java 8而我是新手,我不知道问题是什么。我搜索了大约两天但没有运气。我很感激你的帮助。

现在越界是由ProjectService类引起的,特别是。{ public Project find(Long projectId)...

现在我的服务类是:

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;
import com.spring.project.model.Project;

@Service
public class ProjectService {

    private List<Project> projects = new LinkedList<>();

    public ProjectService(){
        Project javaProject = this.createProject("Java Project", "This is a Java Project");
        Project javascriptProject = this.createProject("Javascript Project", "This is a Javascript Project");
        Project htmlProject = this.createProject("HTML Project", "This is an HTML project");

        this.projects.addAll(Arrays.asList(new Project[]{javaProject,javascriptProject, htmlProject}));
    }

    public List<Project> findAll(){
        return this.projects;
    }

    public Project find(Long projectId){
        return this.projects.stream().filter(p -> {
            return p.getProjectId().equals(projectId);
        }).collect(Collectors.toList()).get(0);
    }

    private Project createProject(String title, String description){
        Project project = new Project();

        project.setName(title);
        project.setAuthorizedFunds(new BigDecimal("100000"));
        project.setAuthorizedHours(new BigDecimal("1000"));
        project.setDescription(description);
        project.setSponsor("NASA");
        project.setYear("2016");
        project.setSpecial(true);
        project.setType("single");

        return project;

    }

}

现在我的控制器看起来像这样:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.spring.project.services.ProjectService;

@Controller
@RequestMapping("/project")
public class ProjectController {


    @Autowired
    private ProjectService projectService;

    @RequestMapping(value="/{projectId}")
    public String findProject(Model model, @PathVariable("projectId") Long projectId){
        model.addAttribute("project", this.projectService.find(projectId));
        return "project";
    }

    @RequestMapping(value="/find")
    public String find(Model model){
        model.addAttribute("projects", this.projectService.findAll());
        return "projects";
    }
}

2 个答案:

答案 0 :(得分:1)

如果没有堆栈跟踪,很难说,但这段代码看起来很可疑:

return this.projects.stream().filter(p -> {
    return p.getProjectId().equals(projectId);
}).collect(Collectors.toList()).get(0);

如果方法调用collect(Collectors.toList())返回空列表,则获取空列表的第一个元素将导致java.lang.IndexOutOfBoundsException。考虑使用 Streams#findFirst返回Optional而非collect(Collectors.toList()).get(0),可以抛出异常。

答案 1 :(得分:-1)

问题在于过滤器中的lambda表达式。从过滤器中删除return语句,然后尝试:

即。你的代码非常冗长,应如下所示:

return this.projects.stream().filter(p -> p.getProjectId().equals(projectId)).findFirst().get();

当您尝试搜索不存在的ID时,您将获得更有意义的java.util.NoSuchElementExceptionget(0)java.lang.IndexOutOfBoundsException的主要原因。