【发布时间】:2017-05-04 22:26:45
【问题描述】:
我已经使用 Spring Boot 和 IntelliJ IDEA 开发了一个 REST API。该 API 已启动并正在运行。 Postman 可以访问它并提供相关的 JSON 响应。
然后我尝试从 C# windows 窗体应用程序访问它。我试图将其添加为网络参考。但是该按钮已被禁用。 我的代码有什么问题?
@RestController
@RequestMapping("/students")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping(value="/hello")
String home() {
return "Hello World!";
}
@RequestMapping(value="getAllStudents",method = RequestMethod.GET)
public Collection<Student> getAllStudents(){
return studentService.getAllStudents();
}
@RequestMapping(value = "/{id}",method = RequestMethod.GET)
public Student getStudentById(@PathVariable("id") int id)
{
return studentService.getStudentById(id);
}
@RequestMapping(value = "/{id}" , method = RequestMethod.DELETE)
public void deleteStudentById(@PathVariable("id") int id)
{
studentService.removeStudentById(id);
}
如果我将 URL 指定为 localhost:8080/students/hello,则 JSON 响应会到达 Visual Studio。但它不能作为网络参考添加。
对此有什么解决方案...?
【问题讨论】:
-
你的 C# 代码的主要问题是它的 Java 代码
-
我从未在 .NET 中使用过 REST API,但我始终相信 Visual Studio 中的
Web reference旨在与 SOAP Web 服务一起使用。要使用 REST API,我认为您需要使用HttpClient自行构建它或使用 3rd 方库。
标签: java c# rest intellij-idea spring-boot