A while ago I asked this question in Stackoverflow which helped me be build this spring boot app
At the moment I have created my own object mapper with a SimpleModule.
Then using my object mapper I am serializing the Node object inside the Controller class and I am returning a ResponseEntity<String> based on the depth value that is passed in the request parameter.
What I want to achieve is to let Spring Boot do the serialization as it happens normaly. So from inside my Controller I want to return ResponseEntity<Node> and somehow replace Spring Boots ObjectMapper with mine and do the serialization using the depth request parameter.
Any ideas are more than welcomed.
Thanks in advance
Related
So, I have a spring boot Rest API running (A).
I want to create a separate app(B) which shows the swagger document of A. In order to achieve that I guess I'd have to be able to capture the Swagger json of A, perhaps in a SwaggerResource
springfox.documentation.swagger.web.SwaggerResource
but I don't know how to construct a SwaggerResource object (or similar) from a URL (e.g."http://localhost:9091/swagger-ui.html"). I would have thought this was a trivial enough task, but it has me beat. I'm trying to avoid using eureka.
I have been learning spring-boot for a while and building learning projects step by step. I am currently doing a project, what i am trying to achieve is building a web endpoint which receives an XML data/list as an input and write it to DB.
To make my point clear:
I have a JMS Queue and a working program that reads the Queue, parse it to defined xml format and publish it to an endpoint.
My new project is supposed to listen for XML data, parse it based on a predefined class matching the XML structure (i am thinking of defining the structure with a class) and using JPA to persist an instance of the class(parsed XML) to the database.
Previously i have some experience with basic RESTful web-service projects with GET,POST,DELETE methods.
What i am asking is :
Is my above outline feasible
I can not find a way to implement parsing of an XML to an Object
In the controller class (that's what i've been using for REST) what method do i use as an entry point.
Thank you.
You can use a put or post method in a REST controller that Consumes XML data.
#RestController
#RequestMapping(value = "/myRestPath", consumes = "application/xml")
public class MyXmlController{
#PutMapping
public void putXmlObject(MyXmlObject myXmlObject){
// do somthing
}
}
I'm using Vertx 3, and I'm trying to find a good decoupled module that knows to turn query-string, headers and both content-type and body into a bean?
I know spring does that and various other frameworks as well, but I don't want to introduce a new framework i just want a super fast model binder that will either know to auto bind to a certain method or at least auto bind a certain class so i can invoke my rest method that currently accept one parameter, which is the model.
public ResponseBase query(QueryRequest model){ ... }
I don't mind adding annotations to the parameters etc.
Thanks!
Current my team use vertx Json.decodeValue to turn body (json string) to java class.
MyClass body = Json.decodeValue(rc.getBodyAsString(), MyClass.class);
to config Json to handle unknown properties, I setting
Json.mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
for your query string, I think it is easy to write a class to convert it to a json string :)
I also catch DecodeException on Json.decodeValue to re throw a 400 Bad Request error.
I am currently working on a web project which is using Play Framework 2.1.0. Play supports a decent API for parsing form data and mapping that to the Model beans directly. Which looks something like,
Form<Employee> form = Form.form(Employee.class).bindFromRequest();
if (form.hasErrors()) {
return badRequest(template.render(form));
}
This API also does validations on the fly and is capable of handling binding failures, when say a String could not be converted to an Integer. The Form API keeps the collection of errors mapped to the name of the property. Underlying the Form API, Play is using DataBinder of Spring's validation framework which is actually doing all the magic.
I was wondering if there is similar binding API to convert from JSON to the bean directly, with support for handling binding failures?
Play 2.0 uses Jackson internally which fails when there are binding failures and simply throws an exception. I looked at the code and does not look easy to supress these errors.
Is there some framework that can satisfy my requirement out of the box?
Essentially, I need the framework to convert from JSON to Java Bean, which can handle binding failures gracefully.
It would be wonderful if it allows me to collect them somewhere so I can generate appropriate validation errors. I will run custom validations on the parsed object using javax.validation APIs to perform more specific validations once the JSON is parsed into the Bean.
I achieved this by adding custom deserializers in Jackson
SimpleDeserializers deserializers = new SimpleDeserializers();
deserializers.addDeserializer(Integer.class, new MyIntegerDeserializer(null));
deserializers.addDeserializer(Long.class, new MyLongDeserializer(null));
ObjectMapper mapper = new ObjectMapper().setDeserializerProvider(
new StdDeserializerProvider().withAdditionalDeserializers(deserializers));
MyModel value = mapper.treeToValue(node, MyModel.class);
MyIntegerDeserializer and MyLongDeserializer are custom deserializers for Integer and Long values respectively. These are in my case exact copy of the internal default corresponding deserializer classes with additional code to gracefully handle NumberFormatException
Are there any online tutorial available to use Hibernate validator with Jackson for validating JSON?
I am using Spring and Jackson now for JSON parsing.
With Spring, I use MappingJacksonHttpMessageConverter to automatically translate the incoming JSON payload to Java object.
I would like to add validator annotations to the Java class. For example, if I have the Person class with "name" as one of the fields, I like to validate the JSON payload if the "name" element appears 1 time. If it appears more than 1 time in the payload, then I like the validator to kick in to reject the request.
Right now what I observed is that if there are multiple names in the payload, Jackson simply ignores and picks up the last one.
The Java object is not bound to any XML schema and the like.
What is the best practice for validating JSON?
Spring has JSR-303 support built in, see this chapter in the reference manual.
If you are using Spring MVC, see here, you should just be able to annotate parameters to #RequestMapping methods with #Valid.