0

I have develop a REST API using Spring Boot and IntelliJ IDEA. That API is up and running. It is accessible by Postman & it gives the relevant JSON response.

Then I tried to access it from C# windows form application. I tried to add it as a web reference. But that button has been disabled. What is the problem with my code?

@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);
}

If I gives the URL as localhost:8080/students/hello the JSON response come to the visual studio. But it cannot be added as web reference.

enter image description here

What is the solution for this...?

Chamith
  • 129
  • 1
  • 13
  • Main problem of your C# code is that its Java code – Sergey Berezovskiy May 04 '17 at 14:11
  • 3
    I never consumed REST API in .NET, but I always believed that `Web reference` in visual studio is intended to be use with SOAP web service. To consume a REST API, I think you need to build it on your own with `HttpClient` or use a 3rd party library. – fharreau May 04 '17 at 14:15

1 Answers1

2

Web References in Visual Studio are meant for SOAP/ASMX web services. If you are calling a REST API then, as @fharreau commented you'lld need to use HttpClient.

The link shows you how to do it, but it may be difficult to read/follow:

https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client

This SO answer might be better

https://stackoverflow.com/a/10928807/2309376

In the SO answer you will need to replace the "http://www.contoso.com/" with your URL "http://localhost:8080/students/hello"

Community
  • 1
  • 1
Simply Ged
  • 8,250
  • 11
  • 32
  • 40
  • Thank you @Simply Ged. & fharreau But I used .NET just to consume my web service from a web client application. I tried to add that web service to java web client in netbeans. Same problem there. Netbeans says "Cannot determine if the service is of type WSDL or WADL" – Chamith May 04 '17 at 15:24
  • @Chamith : Are you using WinForms or a web client? Unclear what you ask. Anyway, WSDL is the interface contract for SOAP Web Services. I never heard about WADL (just look about it and IMHO, it is an aberration but that's not the point) and I am not a Java specialist but I assume nothing in your Java code generate such a file. I guess this is why you cannot open it in NetBeans. – fharreau May 04 '17 at 16:46