下载其余对象而不是在Eclipse浏览器中显示

时间:2018-11-12 11:47:24

标签: json spring eclipse

每次我尝试从页面显示结果时,它都会以json格式下载结果,而不是在页面上显示结果。

shows the problem

当我输入存储对象/信息的URL时开始下载,而不显示页面http://localhost:8082/spring-rest-demo/api/students

如果我运行服务器并将此信息粘贴到邮递员google chrome中,它将显示正确的信息,而无需将其下载为json文件。

应该是这样

enter image description here

谢谢

编辑:

package com.luv2code.springdemo.rest;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.luv2code.springdemo.entity.Student;

@RestController
@RequestMapping("/api")
public class StudentRestController {

    private List<Student> theStudents;

    // define @PostConstruct to load the student data only once!

    @PostConstruct
    public void loadData() {

        theStudents = new ArrayList<>();

        theStudents.add(new Student("Poornima", "Patel"));
        theStudents.add(new Student("Mario", "Rossi"));
        theStudents.add(new Student("Mary", "Smith"));

    }

    // define endpoint for "/Student"-- return list of students

    @GetMapping("/students")
    public List<Student> getStudents() {

        return theStudents;
    }

    // define endpoint for "/Student({studentid}"-- return list of students at index

    @GetMapping("/students/{studentId}")
    public Student getStudent(@PathVariable int studentId) {

        // just index into the list .... keep it simple for now

        return theStudents.get(studentId);
    }

}

2 个答案:

答案 0 :(得分:0)

尝试在@GetMapping批注中添加生产application / json。

答案 1 :(得分:0)

这是最新版本的Eclipse中的错误。希望Eclipse小组能尽快解决它 Screenshot of my Spring tool Suit (kind of Eclipse implementation)