Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Chandan Sharma
415: Unsupported media type (while consuming JSON input to JAX-RS service)
October 11, 2017 4:08 AM
Answer

Chandan Sharma

Rank: Junior Member

Posts: 54

Join Date: March 3, 2014

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):

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


Here is my sample code:
 1package org.chandan.leave.rest.application;
 2
 3import java.text.SimpleDateFormat;
 4import java.util.Collections;
 5import java.util.Date;
 6import java.util.HashSet;
 7import java.util.Set;
 8
 9import javax.ws.rs.ApplicationPath;
10import javax.ws.rs.Consumes;
11import javax.ws.rs.GET;
12import javax.ws.rs.POST;
13import javax.ws.rs.PUT;
14import javax.ws.rs.Path;
15import javax.ws.rs.PathParam;
16import javax.ws.rs.Produces;
17import javax.ws.rs.QueryParam;
18import javax.ws.rs.core.Application;
19import javax.ws.rs.core.MediaType;
20
21import org.chandan.leave.exception.NoSuchLeaveException;
22import org.chandan.leave.model.Leave;
23import org.chandan.leave.rest.modal.Employee;
24import org.chandan.leave.rest.modal.Status;
25import org.chandan.leave.service.LeaveLocalService;
26import org.osgi.service.component.annotations.Component;
27import org.osgi.service.component.annotations.Reference;
28
29import com.liferay.portal.kernel.json.JSONFactoryUtil;
30
31@ApplicationPath("/leave")
32@Component(immediate = true, service = Application.class)
33public class LeaveRestControllerApplication extends Application {
34
35    public Set<Object> getSingletons() {
36        return Collections.<Object>singleton(this);
37    }
38    private LeaveLocalService leaveLocalService;
39   
40    @Reference(unbind="-")
41    public void setLeaveLocalService(LeaveLocalService leaveLocalService){
42        this.leaveLocalService=leaveLocalService;
43    }
44
45   
46    @POST
47    @Path("/add-emp")
48    @Consumes(MediaType.APPLICATION_JSON)
49    @Produces(MediaType.APPLICATION_JSON)
50    public Employee addEmp(Employee emp){
51       
52        System.out.println(emp.getFirstName() + " " + emp.getLastName());
53       
54        Employee newEmp = new Employee();
55        newEmp.setFirstName("Chandan");
56        newEmp.setLastName("Sharma");
57       
58        return newEmp;
59    }
60
61}


build.gradle
 1dependencies {
 2    compileOnly group: "javax.ws.rs", name: "javax.ws.rs-api", version: "2.0.1"
 3    compileOnly group: "org.osgi", name: "org.osgi.service.component.annotations", version: "1.3.0"
 4    compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0"
 5    compileOnly group: "com.liferay.portal", name: "com.liferay.util.java", version: "2.0.0"
 6    compileOnly group: "com.liferay.portal", name: "com.liferay.portal.impl", version: "2.0.0"
 7   
 8    compile group: 'org.glassfish.jersey.media', name: 'jersey-media-json-jackson', version: '2.9'
 9    compile group: 'org.glassfish.jersey.media', name: 'jersey-media-moxy', version: '2.9'
10   
11    compileOnly project(":modules:leave-core:leave-core-api")
12    compileOnly project(":modules:leave-core:leave-core-service")
13}
Adam Victor Brandizzi
RE: 415: Unsupported media type (while consuming JSON input to JAX-RS servi
October 11, 2017 4:48 AM
Answer

Adam Victor Brandizzi

LIFERAY STAFF

Rank: New Member

Posts: 13

Join Date: March 11, 2013

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:
1provided group: "org.apache.cxf", name: "cxf-core", version: "3.0.3"
2provided group: "org.apache.cxf", name: "cxf-rt-frontend-jaxrs", version: "3.0.0"
3provided 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
RE: 415: Unsupported media type (while consuming JSON input to JAX-RS servi
October 11, 2017 4:55 AM
Answer

Tomas Polesovsky

LIFERAY STAFF

Rank: Liferay Master

Posts: 654

Join Date: February 13, 2009

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
RE: 415: Unsupported media type (while consuming JSON input to JAX-RS servi
October 11, 2017 7:12 AM
Answer

Chandan Sharma

Rank: Junior Member

Posts: 54

Join Date: March 3, 2014

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