将controller的注解改成@RestController,示例
import com.example.demo.User;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");
}
}
public class User {这样就返回json数据了。
public User(String sico) {
this.setName(sico);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String name;
}
网友回复