Forums

angle-left Back

RE: 415: Unsupported media type (while consuming JSON input to JAX-RS servi

Chandan Sharma, modified 9 Months ago.

415: Unsupported media type (while consuming JSON input to JAX-RS service)

Junior Member Posts: 54 Join Date: 3/3/14 Recent Posts
I am trying to consume JSON as input to JAX-RS service. But I am getting: 415: Unsupported media type.

While searching through Google, got some suggestion for adding below dependency (in build.gradle):

compile group: 'org.glassfish.jersey.media', name: 'jersey-media-json-jackson', version: '2.9'
compile group: 'org.glassfish.jersey.media', name: 'jersey-media-moxy', version: '2.9'


Here is my sample code:
package org.chandan.leave.rest.application;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;

import org.chandan.leave.exception.NoSuchLeaveException;
import org.chandan.leave.model.Leave;
import org.chandan.leave.rest.modal.Employee;
import org.chandan.leave.rest.modal.Status;
import org.chandan.leave.service.LeaveLocalService;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import com.liferay.portal.kernel.json.JSONFactoryUtil;

@ApplicationPath("/leave")
@Component(immediate = true, service = Application.class)
public class LeaveRestControllerApplication extends Application {

    public Set<Object> getSingletons() {
        return Collections.<Object>singleton(this);
    }
    private LeaveLocalService leaveLocalService;
   
    @Reference(unbind="-")
    public void setLeaveLocalService(LeaveLocalService leaveLocalService){
        this.leaveLocalService=leaveLocalService;
    }

   
    @POST
    @Path("/add-emp")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Employee addEmp(Employee emp){
       
        System.out.println(emp.getFirstName() + " " + emp.getLastName());
       
        Employee newEmp = new Employee();
        newEmp.setFirstName("Chandan");
        newEmp.setLastName("Sharma");
       
        return newEmp;
    }

}


build.gradle
dependencies {
    compileOnly group: "javax.ws.rs", name: "javax.ws.rs-api", version: "2.0.1"
    compileOnly group: "org.osgi", name: "org.osgi.service.component.annotations", version: "1.3.0"
    compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0"
    compileOnly group: "com.liferay.portal", name: "com.liferay.util.java", version: "2.0.0"
    compileOnly group: "com.liferay.portal", name: "com.liferay.portal.impl", version: "2.0.0"
   
    compile group: 'org.glassfish.jersey.media', name: 'jersey-media-json-jackson', version: '2.9'
    compile group: 'org.glassfish.jersey.media', name: 'jersey-media-moxy', version: '2.9'
   
    compileOnly project(":modules:leave-core:leave-core-api")
    compileOnly project(":modules:leave-core:leave-core-service")
}
Adam Victor Brandizzi, modified 9 Months ago.

RE: 415: Unsupported media type (while consuming JSON input to JAX-RS servi (Answer)

LIFERAY STAFF New Member Posts: 13 Join Date: 3/11/13 Recent Posts
Hello Chandan! I'm not that well-versed with JAX-RS but I had some ideas/questions. May be they can help emoticon

First of all, what is your Liferay version?
Chandan Sharma:
While searching through Google, got some suggestion for adding below dependency (in build.gradle):

Well, I never needed these dependencies when working with JAX-RS on portal. OTOH I needed Apache CXF, so I used these dependencies:
provided group: "org.apache.cxf", name: "cxf-core", version: "3.0.3"
provided group: "org.apache.cxf", name: "cxf-rt-frontend-jaxrs", version: "3.0.0"
provided group: "org.apache.cxf", name: "cxf-rt-rs-extension-providers", version: "3.0.3"

Chandan Sharma:
Here is my sample code:

Is your Employee class annotated with @XmlRootElement? Could you post it here?
Tomas Polesovsky, modified 9 Months ago.

RE: 415: Unsupported media type (while consuming JSON input to JAX-RS servi

LIFERAY STAFF Liferay Master Posts: 654 Join Date: 2/13/09 Recent Posts
Hi guys,

just a quick question, what "Accept" and "Content-Type" header do you use in your HTTP request from client? It should be application/json !
Chandan Sharma, modified 9 Months ago.

RE: 415: Unsupported media type (while consuming JSON input to JAX-RS servi

Junior Member Posts: 54 Join Date: 3/3/14 Recent Posts
It worked @Adam Victor Brandizzi after adding @XmlRootElement

Adam Victor Brandizzi:

Is your Employee class annotated with @XmlRootElement? Could you post it here?


It didn't required to add Apache CXF dependencies. Although I tried to add it but got Build failed.

Thanks you for your helpemoticon

And coming to

Tomas Polesovsky:
Hi guys,

just a quick question, what "Accept" and "Content-Type" header do you use in your HTTP request from client? It should be application/json !


Yes I had used it "Accept" and "Content-Type" header with value application/json, while making request earlier.

Thanks @Thomas for your help

~Regards,
Chandan