Forums de discussion
415: Unsupported media type (while consuming JSON input to JAX-RS service)
Chandan Sharma, modifié il y a 6 années.
415: Unsupported media type (while consuming JSON input to JAX-RS service)
Junior Member Publications: 54 Date d'inscription: 04/03/14 Publications récentes
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):
Here is my sample code:
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;
}
}<br><br><strong>build.gradle</strong><br><pre><code>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")
}</code></pre></object></object>
Adam Victor Brandizzi, modifié il y a 6 années.
RE: 415: Unsupported media type (while consuming JSON input to JAX-RS servi (Réponse)
New Member Publications: 13 Date d'inscription: 11/03/13 Publications récentes
Hello Chandan! I'm not that well-versed with JAX-RS but I had some ideas/questions. May be they can help
First of all, what is your Liferay version?
Well, I never needed these dependencies when working with JAX-RS on portal. OTOH I needed Apache CXF, so I used these dependencies:
Is your Employee class annotated with @XmlRootElement? Could you post it here?
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, modifié il y a 6 années.
RE: 415: Unsupported media type (while consuming JSON input to JAX-RS servi
Liferay Master Publications: 676 Date d'inscription: 13/02/09 Publications récentes
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 !
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, modifié il y a 6 années.
RE: 415: Unsupported media type (while consuming JSON input to JAX-RS servi
Junior Member Publications: 54 Date d'inscription: 04/03/14 Publications récentes
It worked @Adam Victor Brandizzi after adding @XmlRootElement
It didn't required to add Apache CXF dependencies. Although I tried to add it but got Build failed.
Thanks you for your help
And coming to
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
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 help
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