1

I am learning how to use Restful Webservice, i dont really know if my approach is good or totally wrong, so bear with me

I got a project structure, which look like this :

enter image description here

I want to by calling the right URL, save the string accordingly in Datum.Json

Here is my Java Class of the WebService :

package Rest;

@Path("/calendar")
public class CalendarTest {
    public List<Date> dates;
    @GET
    @Path("/dates/get/")
    @Produces(MediaType.APPLICATION_XML)
    public List<Date> getUsers(){
       return dates;
    }
    @PUT
    @Path("/dates/put/{param1}+{param2}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public void updateDate(@PathParam("param1") String city, @PathParam("param2") String date) throws IOException {    
        JSONObject obj = new JSONObject();
        obj.put("City", city);
        obj.put("Date", date);
        try (FileWriter file = new FileWriter("/RestTest/Datum.json")) {
            file.write(obj.toJSONString());
            System.out.println("Successfully Copied JSON Object to File...");
            System.out.println("\nJSON Object: " + obj);
        }

    }   
}

I tested the localhost, it works fine (i can open the form.html with my localhost) My web.xml file :

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Restful Web Application</display-name>

    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>
                    org.glassfish.jersey.servlet.ServletContainer
                </servlet-class>
        <init-param>
             <param-name>jersey.config.server.provider.packages</param-name>
             <param-value>Rest</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
    <security-constraint>

</security-constraint>
</web-app>

But when i tried the URL http://localhost:8080/RestTest/rest/calendar/dates/put/Berlin+20-12-2019

It says the method not allowed.

Can anyone explain to me why ?

Tuấn Phạm
  • 688
  • 6
  • 20

1 Answers1

3

It's because when you are typing in a URL in your browser, it sends a HTTP GET request by default, so it throws an error because you do not have a GET request handler for that URL, you only have a handler for a PUT request.

You can't change the browser's default request type. What you have to do is send the request yourself using something like jQuery in your frontend / javascript.

How to send a PUT/DELETE request in jQuery?

You could use the ajax method:

$.ajax({
    url: '/rest/calendar/dates/put/Berlin+20-12-2019',
    type: 'PUT',
    success: function(result) {
        // Do something with the result
    }
});
Community
  • 1
  • 1
tima
  • 1,498
  • 4
  • 20
  • 28
  • so how could i access to the Put method ? Thanks! – Tuấn Phạm May 14 '17 at 14:05
  • @TuấnPhạm one option is jQuery, there is also the AngularJS framework. And if you use POST instead of PUT, you can use an HTML form to submit the request – tima May 14 '17 at 14:09