Get request using JAX-RS Servlet unsupported media error

时间:2018-12-19 11:31:05

标签: java jax-rs

Using JAX-RS in eclipse, I am getting error

HTTP ERROR 415.

If I remove "/{TR_ID]" from "@Path("/TRData/{TR_ID}")" and do not TR_ID in select statement, it works fine and returns rows. I want to limit returned rows to a single one that matches TR_ID in database. Value of TR_ID is give by user in url for example ~/TRData/330377

My class file is as follows:

@Path("/TRData/{TR_ID}")
    @GET
    @Produces({ MediaType.APPLICATION_JSON })
    @Consumes({ MediaType.APPLICATION_JSON})
    public String getTRData(@PathParam("TR_ID") String TRID, @Context HttpServletRequest request, HttpServletResponse response) throws JSONException {

        String PNo = UMFactory.getAuthenticator().getLoggedInUser().getUniqueName();
        String TR_ID = TRID;


        try {

            System.err.println("Start TR Details");
            Class.forName("oracle.jdbc.driver.OracleDriver");

Connection con = DriverManager.getConnection("jdbc:oracle:thin:@ip:1521:mydb", "my_user", "myPassword");



String TRQuery="SELECT VTR.TRANSPORT_REQUEST_ID, VTR.PNO, (SELECT NAME FROM MR_HRS_EMPLOYEE WHERE REG_NO = VTR.PNO AND EMPLOYER_CODE = VTR.EMPLOYER_CODE) EMP_NAME, (SELECT DESIG_SHORT FROM MR_HRS_EMPLOYEE WHERE REG_NO = VTR.PNO AND EMPLOYER_CODE = VTR.EMPLOYER_CODE) DESIG_SHORT, VTR.STATUS, VTR.DRIVER_REQUIRED, VTR.PURPOSE_OF_VISIT, VTR.RECOMMENDED_BY, VTR.EMPLOYER_CODE, VTR.LOCATION_ID, VTR.FROM_TIME, VTR.TO_TIME, VTR.FROM_DATE, VTR.TO_DATE, VTR.CONTACT_NUMBER, VTR.AIRPORT_REQUEST, VTR.FLIGHT_NO, VTR.REQUEST_TYPE, VTR.REJECTED_BY, VTR.REMARKS, VTR.NO_OF_PASSENGERS FROM VMS.VMS_TRANSPORT_REQUEST VTR WHERE     VTR.STATUS = 'I' AND VTR.REQUEST_TYPE = 'O' AND VTR.DELETE_FLAG IS NULL AND VTR.RECOMMENDED_BY = " + PNo + " AND TRANSPORT_REQUEST_ID = " + TR_ID + " AND LOCATION_ID = (SELECT LOCATION_ID FROM MR.MR_HRS_EMPLOYEE WHERE REG_NO = " + PNo + ")";

            // Get AllTR Data from DB & Put it into JSON
                        System.err.println("Getting TR Data");
                        Statement TRStatement = con.createStatement();
                        ResultSet TRRecordSet = TRStatement.executeQuery(TRQuery);


                        ResultSetMetaData rsmd = TRRecordSet.getMetaData();

                        JSONArray TRjson = new JSONArray();

                        while (TRRecordSet.next()) {

                            JSONObject obj = new JSONObject();

                            int numColumns = rsmd.getColumnCount();

                            for (int i = 1; i <= numColumns; i++) {
                                String column_name = rsmd.getColumnName(i);
                                if (rsmd.getColumnType(i) == java.sql.Types.ARRAY) {
                                    obj.put(column_name, TRRecordSet.getArray(column_name));
                                } else if (rsmd.getColumnType(i) == java.sql.Types.BIGINT) {
                                    obj.put(column_name, TRRecordSet.getInt(column_name));
                                } else if (rsmd.getColumnType(i) == java.sql.Types.BOOLEAN) {
                                    obj.put(column_name, TRRecordSet.getBoolean(column_name));
                                } else if (rsmd.getColumnType(i) == java.sql.Types.BLOB) {
                                    obj.put(column_name, TRRecordSet.getBlob(column_name));
                                } else if (rsmd.getColumnType(i) == java.sql.Types.DOUBLE) {
                                    obj.put(column_name, TRRecordSet.getDouble(column_name));
                                } else if (rsmd.getColumnType(i) == java.sql.Types.FLOAT) {
                                    obj.put(column_name, TRRecordSet.getFloat(column_name));
                                } else if (rsmd.getColumnType(i) == java.sql.Types.INTEGER) {
                                    obj.put(column_name, TRRecordSet.getInt(column_name));
                                } else if (rsmd.getColumnType(i) == java.sql.Types.NVARCHAR) {
                                    obj.put(column_name, TRRecordSet.getNString(column_name));
                                } else if (rsmd.getColumnType(i) == java.sql.Types.VARCHAR) {
                                    obj.put(column_name, TRRecordSet.getString(column_name));
                                } else if (rsmd.getColumnType(i) == java.sql.Types.TINYINT) {
                                    obj.put(column_name, TRRecordSet.getInt(column_name));
                                } else if (rsmd.getColumnType(i) == java.sql.Types.SMALLINT) {
                                    obj.put(column_name, TRRecordSet.getInt(column_name));
                                } else if (rsmd.getColumnType(i) == java.sql.Types.DATE) {
                                    obj.put(column_name, TRRecordSet.getDate(column_name));
                                } else if (rsmd.getColumnType(i) == java.sql.Types.TIMESTAMP) {
                                    obj.put(column_name, TRRecordSet.getTimestamp(column_name));
                                } else {
                                    obj.put(column_name, TRRecordSet.getObject(column_name));
                                }

                            }
                            TRjson.put(obj);

                        }

                        JSONObject mainObj = new JSONObject();

                        mainObj.put("trdata", TRjson);
            return mainObj.toString();

        }
        catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return e.getMessage() + "EX-MESSAGE";
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return e.getMessage() + "EX-MESSAGE";
        }

1 个答案:

答案 0 :(得分:0)

您在HttpServletResponse旁边缺少@Context。这是注射所需的。另外,它是一个GET方法;不需要@Consumes,因为该方法不消耗任何东西。