Hassan Shahbaz
Hassan Shahbaz

Reputation: 606

The requested resource is not available Java REST API

I have created a simple Rest Api that return id's stored in database but when i type url in browser it shows error

Here is what i am doing

package com.comicsmv.api;

//Imports here

@Path("/files")
public class Api {

    private static final String SERVER_UPLOAD_LOCATION_FOLDER = "/home/hassan/Downloads/";

    /**
     * Upload a File
     * 
     * @throws IOException
     */

    @POST
    @Path("/upload")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response responseMsg(String obj) {
        String object = obj;

        JSONObject jobj = null;
        try {
            jobj = new JSONObject(object);
        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        String api_key = null;
        try {
            api_key = jobj.getString("api_key");
        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        File f = new File("home/hassan/api_key.txt");
        String file_name = f.getName();
        String key = readFile(file_name);
        if (api_key != key) {
            return Response.status(400).entity("Api key does not found/match")
                    .build();
        } else {
            try {
                String img = jobj.getString("image");

                BufferedImage image = null;
                byte[] imageByte;

                String file_path = System.getProperty("user.dir");
                File directory = new File(file_path + "/uploads");
                boolean exists = directory.exists();
                if (!exists) {
                    directory.mkdir();
                }

                try {
                    BASE64Decoder decoder = new BASE64Decoder();
                    imageByte = decoder.decodeBuffer(img);
                    ByteArrayInputStream bis = new ByteArrayInputStream(
                            imageByte);
                    image = ImageIO.read(bis);
                    bis.close();
                    java.util.Date date = new java.util.Date();
                    String outputfile = directory.toString() + "/"
                            + new Timestamp(date.getTime()) + ".png";
                    // System.out.println(outputfile);
                    ImageIO.write(image, "png", new File(outputfile));

                } catch (Exception e) {
                    e.printStackTrace();
                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return Response.status(200).entity(obj).build();
        }
    }
}

And here is web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Comicsmv_Api</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
        <servlet-name>jersey-helloworld-serlvet</servlet-name>
        <servlet-class>
                     com.sun.jersey.spi.container.servlet.ServletContainer
                </servlet-class>
        <init-param>
             <param-name>com.sun.jersey.config.property.packages</param-name>
             <param-value>com.comicsmv.api</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-helloworld-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

But when i type url like (localhost:8080/Comicsmv_Api/rest/files/upload) then it show 404 Status that requested source is not available.

Upvotes: 1

Views: 2334

Answers (1)

LeTex
LeTex

Reputation: 1452

the problem is:

there is no REST API that matches this request:

GET localhost:8080/Comicsmv_Api/rest/files/upload

what you have is :

POST localhost:8080/Comicsmv_Api/rest/files/upload
+ { JSON payload }

Remember the browser always send a GET request. In order to send other HTTP methods request use tools like below that lets you choose all HTTP methods:

  • postman (chrome extention)
  • restclient (Firefox debugger add on)
  • soapUI (open-source web service testing application)

Upvotes: 1

Related Questions