I am not able to read json data in post. So far I have been successful is reading string data in get method. Now i am working on POST where I am sending following JSON in body :
{"name": "myname","address":"myaddress"}
I am also sending content-type : application/json header information in the request.
I have downloaded jersey moxy jar and corresponding dependencies. My model class is as following :
#XmlRootElement
public class Mymodel{
#XMLElement
public String name;
#XMLElement
public String address;
}
following is the extract of the resource method
#POST
#Consumes(MediaType.Application_JSON)
public Resource mymethod(Mymodel mode){
System.out.println(mode.name);
}
Am i missing something ? Error I am getting is that media type is not supported(415 Unsupported Media Type).
I don't have anything related to json in the web.xml.
As i mentioned in my post above ,I am using jersey moxy jar which actually supports json application type in jersey. So my understanding is that as suggested adding "jersey-media-json-jackson" jar will not add any value, or I need both jackson and moxy jar ?
Related
I have looked at the various answers and they do not resolve my issue. I have a very specific client need where I cannot use the body of the request.
I have checked these posts:
Trying to use Spring Boot REST to Read JSON String from POST
Parsing JSON in Spring MVC using Jackson JSON
Pass JSON Object in Rest web method
Note: I do encode the URI.
I get various errors but illegal HTML character is one. The requirement is quite simple:
Write a REST service which accepts the following request
GET /blah/bar?object=object11&object=object2&...
object is a POJO that will come in the following JSON format
{
"foo": bar,
"alpha": {
"century": a,
}
}
Obviously I will be reading in a list of object...
My code which is extremely simplified... as below.
#RequestMapping(method=RequestMethod.GET, path = "/test")
public Greeting test(#RequestParam(value = "object", defaultValue = "World") FakePOJO aFilter) {
return new Greeting(counter.incrementAndGet(), aFilter.toString());
}
I have also tried to encapsulate it as a String and convert later which doesnt work either.
Any suggestions? This should really be extremely simple and the hello world spring rest tut should be a good dummy test framework.
---- EDIT ----
I have figured out that there is an underlying with how jackson is parsing the json. I have resolved it but will be a write up.. I will provide the exact details after Monday. Short version. To make it work for both single filter and multiple filters capture it as a string and use a json slurper
If you use #RequestParam annotation to a Map<String, String> or MultiValueMap<String, String> argument, the map will be populated with all request parameters you specified in the URL.
#GetMapping("/blah/bar")
public Greeting test(#RequestParam Map<String, String> searchParameters) {
...
}
check the documentation for a more in depth explanation.
I have method like,
#POST
#Produces(MediaType.APPLICATION_XML)
#Consumes("text/plain")
public File addFile(String filePath){
return fileService.addFile(filePath);
}
And I am using "Postman rest client" for testing the post request and simply type a path like c:\myFile.txt in the raw section
but no String is passed to method, but when I hard-code the path it works
Is the problem from #consume ?
Please see the answer below
#POST
#Produces(MediaType.APPLICATION_XML)
#Consumes(MediaType.TEXT_PLAIN)
public File addFile(String filePath){
return fileService.addFile(filePath);
}
And the header content type
as text/plain while sending the request to server from postman
for your reference please see the image below
As stated above, there is no annotation to fetch the body of the request with Jax-RS ; The original definition of the service is correct.
The problem probably comes from postman settings.
You should select Body > raw > Text (text/plain).
I'm using eBay notification API, i'm getting a SOAP web request that contains XML inside.
I would like to use jersey framework in order to deserialize the request into an object.
Is there any easy way to generate a bean class from the SOAP request that will be used in a simpler way that I'm using json? for an example :
#POST
#Path("/path")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public Response methodName(Item bean)
{
...
}
You can generate bean class from xml response using JAXB library.
Read More about this
Hi I'm trying to return Json from a method in a simple rest using Jersey
HelloWorldService.class
#Path("/hello")
public class HelloWorldService {
#GET
#Path("/empdetail/{id}")
#Produces(MediaType.APPLICATION_JSON)
public EmpDetailsVo getEmpDetails(#PathParam("id") String id){
EmpDetailsVo details = new EmpDetailsVo();
details.empId="1202";
details.empName="Akhi";
details.empAddress="123 Main St. Newark PA 19121";
return details;
}
}
EmpDetailsVo class has getter and setters for empId, name and address.
When I try to run this url:
http://localhost:8080/jerseyRest/rest/hello/empdetail/1202
I get Http status 500.
And on console I see an error:
SEVERE: A message body writer for Java class jerseyRest.EmpDetailsVo, and Java type class jerseyRest.EmpDetailsVo, and MIME media type application/json was not found
And
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class jerseyRest.EmpDetailsVo, and Java type class jerseyRest.EmpDetailsVo, and MIME media type application/json was not found
Can someone please help.
You need to tell Jersey how to marshal and unmarshal objects from the EmpDetailsVo type into JSON.
Check this tutorial for an example of how this can be done. Here's another example using a different approach. Investigate the use of the com.sun.jersey.api.json.POJOMappingFeature parameter provided to your web application via web.xml, and that should get you there.
Can you point me to article or explain me how to declare RESTful web service which consumes JSON request and based on parameter inside JSON produces output in different formats, meaning customer can get output in JSON but in pdf also. I'm using Java and RestEasy on JBoss 5.1.
You could map the request on a method returning a RestEasy Response object, using a ResponseBuilder to build your response, setting dynamically the mime type of the response depending on a parameter in your JSON.
#POST
#Path("/foo")
#Consumes("application/json")
public Response fooService(MyObject obj) {
MyResponseEntity entity = MyObjectService.retrieveSomethingFrom(obj);
return Response.status(200).entity(entity).type(obj.isXml() ? "text/xml" : "application/json").build();
}
This way if your MyObject domain object that represent incoming JSON has a parameter xml set to true, then the Response object is parameterized to produce text/xml otherwise it produces application/json. RestEasy should do the rest.
You can use this way
#Path("/")
public class Test {
#Path("/test")
#POST
#Consumes("application/json")
#Produces("text/plain")
public Response addOrderJSON(OrderDetails details) {...}
}