Liferay Jax-RS : Unable to call simple REST apis, getting 403 and 405

Viewed 494

I am using Liferay 7.3.

Trying to build Rest APIs using JAX-RS and I have generated token by following official documentation here https://help.liferay.com/hc/en-us/articles/360018166411-JAX-RS#using-oauth-20-to-invoke-a-jax-rs-web-service

getting 403 [Forbidden] for @GET and 405 [method not allowed] for @POST

package com.liferay.jaxrstest.application;

import java.util.Collections;
import java.util.Set;

import javax.ws.rs.*;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.jaxrs.whiteboard.JaxrsWhiteboardConstants;

@Component(
    property = {
        JaxrsWhiteboardConstants.JAX_RS_APPLICATION_BASE + "=/greetings",
        JaxrsWhiteboardConstants.JAX_RS_NAME + "=Greetings.Rest"
    },
    service = Application.class
)
public class LiferaxJaxRsTestApplication extends Application {

    public Set<Object> getSingletons() {
        return Collections.<Object>singleton(this);
    }

    @GET
    @Produces("text/plain")
    public String working() {
        return "It works!";
    }

    @GET
    @Path("/morning")
    @Produces("text/plain")
    public String hello() {
        return "Good morning!";
    }

    @POST
    @Path("/morning")
    // @Produces("text/plain")
    // @Consumes("application/json")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String hello(User user) {
        return "Good morning!" + user.getFirstName();
    }
}

class User {

    private String firstName;
    
    public User(String firstName) {
        this.firstName = firstName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    
} 

enter image description here

1 Answers

If you are trying to test one of your created endpoints you should use the path created to it.

To test the good morning GET endpoint you should do a GET request to the URL:

http://localhost:8080/o/greetings/morning

…with no Body. As you are using OAuth2, you have to pass the token using the Authorization tab in Postman. There, you can configure how to get a new token and to use it.

Image of how you should configure it

I am also considering that in your OAuth2 configuration in the Control Panel of the Portal you checked the box for GET and POST for the Greetings.Rest service in the Scopes tab.

Finally, to get the clientId and the clientSecret, go to

Control Panel → Configuration → OAuth2 Administration.

Related