+
80
-

spring boot如何返回json数据?

spring boot如何返回json数据?


网友回复

+
0
-

将controller的注解改成@RestController,示例

import com.example.demo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ReController {

    @GetMapping("/user/{id}")
    @ResponseBody
    public User getUserInfo(@PathVariable("id") String id){
        return new User("bfw");
    }
}

User

public class User {
    public User(String sico) {
        this.setName(sico);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private String name;
}

 这样就返回json数据了。
我知道答案,我要回答