I am trying to build a REST service using Spring 4.3.4 and Spring Boot 1.4.2. What I need to do is to configure the Jackson instance in such a way that I can access the actual configuration of the object mapper (e.g. use fields only, don't use getter/setter methods etc).
After a long session of debugging Spring code, I tried all of the following:
#Bean for Jackson2ObjectMapper
#Bean for Jackson2ObjectMapperBuilder
#Bean for Jackson2ObjectMapperFactoryBean
None of the above had any effect. Ultimately, I think I've figured out the root cause of it all. Here's how Spring really creates it's Jackson instances:
AllEncompassingFormHttpMessageConverter calls new MappingJackson2HttpMessageConverter()...
... which builds a new Jackson2ObjectMapperBuilder...
...via static method Jackson2ObjectMapperBuilder.json()
ALL of the above is static (!!) and completely neglects any kind of configuration, regardless whether it's XML, Java #Configuration classes or even application.properties.
The question is: do I need to implement my own converter that replaces the AllEncompassingFormHttpMessageConverter (is that even possible?), or is there any other solution to configure the Jackson instance?
Related
I am trying to develop a microservice using Spring MVC and Spring Boot. In my service I am giving result back as JSON encoded format. Currently I added action like:
#RequestMapping("/checkUsers")
public String checkLogin() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
List<Users> useObj = (List<Users>) userRepo.findAll();
return(mapper.writeValueAsString(useObj));
}
And now I found the other options through the following method
produces={"application/json; charset=UTF-8"}
Here I am not sure which method is properly using for encoding the data into JSON format in Spring. How can I proceed?
Why don’t you just use
ResponseEntity
And return
new ResponseEntity<String>(“your message”, HttpStatus.OK);
#RequestMapping(method = GET, value = "my/random/uri", produces = "application/json;charset=UTF-8")
Here produces attribute inside my #RequestMapping annotation is the producible media types of the mapped request. The format is a single media type or a sequence of media types, with a request only mapped if mentioned one matches one of these media types. and here, my #RequestMapping annotation is already serving my purpose of encoding.
Now somewhere from Spring Boot documentation:
Spring Boot is a brand new framework designed to simplify the bootstrapping and development of a new Spring
application. The framework takes an opinionated approach to
configuration, freeing developers from the need to define boilerplate
configuration.
Please don't kill the purpose of Spring Boot. :)
Additionally, I would suggest you use org.springframework.http.MediaType class. It has got all encoding types you'll ever require. Happy coding.
Please let me know if you face any difficulty while going with suggested approach.
If you use #RestController on your controller, all request mappings will produce application/json by default.
Also, there is no need to to the object mapping yourself, just return the object without mapping and let Spring/Jackson do its thing.
#RestController
public class UsersController {
// #RequestMapping("/checkUsers")
// There is an even simpler and more concise
#GetMapping("/checkUsers")
public List<User> checkLogin() throws JsonProcessingException {
return userRepo.findAll();
}
}
I'm writing a command line app that needs to serialize/deserialize a json file but can't seem to find a non-web example. All the code I found is to do with Restful API and Web.
So the question is how to set up serializer/deserializer and map pojos to json in a Spring boot command line app. It'd be great if there is a sample unit test that shows how to set up its Spring content. I tried the code snippet in Spring boot docs using #JsonTest Auto-configured JSON tests but it complains of not finding matching implementation.
Many thanks
Take a look at the Jackson project, specifically the ObjectMapper.
https://github.com/FasterXML/jackson
https://fasterxml.github.io/jackson-databind/javadoc/2.7/com/fasterxml/jackson/databind/ObjectMapper.html
The simplest way would be to create an ObjectMapper as a bean, and use that throughout your project.
#Configuration
public class MyConfig() {
#Bean
public ObjectMapper objectMapper() {
return new ObjectMapper() ;
}
}
I have a large object which can be updated in a few steps. I'am facing a partial binding problem. My service consumes json and I can not use #InitBinder along with #RequestBody. Cutting this object to a few small ones is not good a solution, because there is a lot of cross-field validations between steps.
Do you have any ideas how to solve this? I'am looking for a clean solution like: registering a specific object mapper for given #RequestMapping or something like that. Thanks for help.
You should be able to use a PATCH HTTP method
Its the preferred method that you would use when you need partial updates, like in your case when you want to update just a few fields of a resource
Spring MVC added a support for it in the version 3.2, so you can do something like
#RequestMapping(value="/patch", method=RequestMethod.PATCH, consumes=MediaType.APPLICATION_JSON_VALUE)
public #ResponseBody String patch(#RequestBody Foo foo) {
return foo.toString();
}
and when sending the request, add only the properties you want to update to your PATCH request, the properties that are null or omited won't be updated
In the lack of better Spring MVC PATCH reference, I'm linking this SO thread as an interesting read Spring MVC PATCH method: partial updates
I have created a Jersey 2.5 Scala REST API Project.
I have a ResourceConfig file, we will call it MyApplication, that looks similar to this:
class MyApplication extends ResourceConfig {
packages(classOf[MyResource].getPackage().getName())
}
All it does is register the resource: MyResource. How can I configure Jersey (2.5) to provide out-of-the-box style JSON Serialization/Deserialization.
For example, here is what MyResource might look like:
#Path("/")
class MyResource {
#POST
#Produces(Array("application/json"))
#Consumes(Array("application/json"))
def getIt(request:SomeRequestModel) = {
/* Do something with the request, return some response model */
return new SomeResponseModel
}
}
So to reiterate, how can I configure Jersey to automatically deserialize and serialize the request and response models, respectively?
It's not actually Jersey that provides the serialisation, it simply draws on an implementation of JAX-RS to perform that role.
Assuming Jersey is a strict requirement, the easiest solution here is to use jackson with Scala bindings. You can find an example here: https://bitbucket.org/jordipradel/jersey-scala-example
If you're not completely tied to Jersey... Might I suggest trying either Spray or spray2-mini instead for a far more idiomatic Scala solution?
I have used Jersey little when developing Java REST services. However, I would say you appear to be conflating two concepts--registering JAX-RS providers and configuring providers to serialize/deserialize JSON.
To register a provider, you use ResourceConfig as you have done.
As for the second issue of configuring Jersey to "know" how to serialize/deserialize JSON:
"As stated in Section 4.3, “Auto-Discoverable Features” JSON-Processing media module is one of the modules where you don't need to explicitly register it's Features (JsonProcessingFeature) in your client/server Configurable as this feature is automatically discovered and registered when you add jersey-media-json-processing module to your classpath."
To add the Jackson flavor of that module to the classpath, you just manually put it there or do this with Maven:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.5</version>
</dependency>
I chose Jackson because it is in my view the best JSON serializer/deserializer in the Java realm.
Having said all this, I once experimented writing services with my preferred Java REST framework, RESTEasy, in Scala. It was more awkward than my first date. Scala and RESTEasy just don't fit together because of an idiom mismatch, issues with types, and so on.
If you want to write REST services in Scala, please consider frameworks built with the language in mind like Scalatra, Unfiltered, or Spray.
I'm working on a RESTful Web-service using Jersey v1.9.1. Some methods return JSON. When I want to Debug my application I start within a grizzly server, otherwise for production I build a war file and place it in a TomCat v7 installation. My projects are all Maven2 projects.
Now, I noticed that for a method that returns List<CustomObj>, where CustomObj has appropriate JAXB annotations, i.e. #XmlRootElement(name="CustomObj"), and getter/setters for all relevant members:
Using grizzly, I get something like {"CustomObj":[{<fields-of-customObj>},{<fields-of-customObj>},{<fields-of-customObj>}]} (when the list has 3 elements). Parsing this with GSON works fine.
Using TomCat, however, I get this: [{<fields-of-customObj>},{<fields-of-customObj>},{<fields-of-customObj>}] -> so as you can see, the "root" is missing somehow
I have the impression that the jersey-json module (which I included into my Maven2 dependencies) are not used at all in TomCat, even though they should be used (they are used in Grizzly for sure). Also, creating my own #Provider for a ContextResolver<JAXBContext> as described here only works in grizzly, in TomCat the getContext() method will never be called.
Is there anything I need to consider with TomCat?
Cheers!