Spring boot. Map request to object if field isnt there - java

Suppose I make a get request to foo() with the JSON body:
{
„a“: „hallo“,
„b“: „world“,
„aa“: 10
}
#PostMapping("/foo")
public void foo(#RequestBody MyClass myClass) {
//do stuff
}
class MyClass {
String a;
String b;
String c;
int aa;
}
If the JSON object and the class match, then it works fine and you get a MyClass object with the correct variables set. But now I want to set only those fields in the MyClass class that I have also passed and the rest is initialized with null. How can I do this?

you have to change #GetMapping to #PostMapping if you want to use #RequestBody.
-Greetings :)

Related

Create Generic class/method to map one object to another

Since I'm a newbie, I would like to know if there is a better way to code this.
Let say we have batch (spring) where we have downloader/processor/mapper/writer for every type of file we receive since we have customized logic for each file type. X number of Mapper , X number of processor for X number of file types.
Currently looking into templatize the code so not much changes may be required when new type is introduced. Below is my idea. so let say mapper, we have different objects for different file types and all of them will be converted to object of Class CustomObject as below. mapper bean in sample spring context
bean id = "file1Mapper" class = "com.filemapper.file1Mapper"
and it invokes file1Mapper class which has mapping logic. Same for other files.
This is what I'm coming up with to avoid all those file1mapper, file2mapper...... instead one generic mapper which does all together, but looking for better solutions,
public class GMapper{
public <T> CustomObject map(T item){
CustomObject customObject = new CustomObject()
.WithABCDetails(getABCDetails(item));
}
private <T> XYZDetails getABCDetails(T item) {
ABCDetails details = new ABCDetails();
if( item instanceof A){
A a = (A)item;
// read a and map it to ABCDetails object
}
if( item instanceof B){
B b = (B)item;
// read b and map it to ABCDetails object
}
...
...
// repeat this if loop for mapping all file types.
return details;
}
}
Sample jsons
class ABCDetails{
// JsonProperty
Object1 ob1;
Object2 ob2;
Integer d;
}
class Object1{
// JsonProperty
Object3 ob3;
String abc;
String def;
}
class Object2{
// JsonProperty
String ab;
Integer e;
}
class A{
// JsonProperty
String e;
String d; // ex, this is mapped to Object 2 String "ab"
}
This does't look so professional and I believe there might be better ways to do it. Can someone please share an example or explanation on how can this code be made better. I also reading Functional interface to see if that could help.
Thanks in advance.
It is impossible to understand what you need. So I will give some common advice.
Format your code - use tabs/spaces to indent.
Do not put capital letters together - replace ABCDetails with AbcDetails. No one cares how real world name looks like.
Do not write meaningless comments - say no to // JsonProperty
Name variables so that someone can understand what they are supposed to store - avoid {Object1 ob1; Object2 ob2; Integer d;}
Do not write if ... else if ... else if ... or case when ... since this scales badly. Use Map. Examples below.
And a general solution to your problem: use plugin architecture - the best thing (and maybe the only thing) that OOP can offer. Just make all your processors implement common interface. And to work with plugins use dispatcher pattern.
First create all processors.
public interface FileProcessor {
String extension();
void process(String filename);
}
#Component
public final class CsvFileProcessor implements FileProcessor {
public String extension() {
return "csv";
}
public void process(String filename) {
/* do what you need with csv */
}
}
#Component
public final class JsonFileProcessor implements FileProcessor {
public String extension() {
return "json";
}
public void process(String filename) {
/* do what you need with json */
}
}
Then inject them into your dispatcher. Do not forget to process errors, for example, some files may not have suffix, for some files you will not have processor, etc.
#Component
public final class FileDispatcher {
private final Map<String, FileProcessor> processorByExtension;
#Autowired
public FileDispatcher(List<FileProcessor> processors) {
processorByExtension = processors.stream().collect(Collectors.toMap(p -> p.extension(), p -> p));
}
public void dispatch(String filename) {
String extension = filename.split("//.")[1];
processorByExtension.get(extension).process(filename);
}
}
Now if you need to support new file format you have to add only one class - implementation of FileProcessor. You do not have to change any of already created classes.

How can I get class object value if given a variable in the form of a string

I have a class
#lombok
class a {
private String status;
}
and I have a method that accepts a string value
public string getValue (String input, Class a) {
// Let's say input value is status
return a.getStatus();
}
How can I return a.getStatus()?
I'm not able to figure out a way to map these values with and without using reflection.
I can change status to getStatus as well in the input if it helps.
What is the input parameter in the getValue function used for? Its not doing anything in your code.
If you simply want to get the value of the class objects variable, you can make that variable static and access it that way.
Use class as following :
#Data
class A {
private String status;
}
The method to return string value :
public String getValue(String input) {
A a = new A();
a.setStatus(input);
return a.getStatus();
}

Get a #JsonProperty by name

I have a Jackson annotated Java class
public class MyClass {
#JsonProperty String foo;
#JsonProperty Optional<Integer> bar;
}
This class is created by Spring Boot from the JSON of a HTTP request body. Some fields, such as bar, might not be present in the JSON. The JSON is not available at this stage.
I would like to fetch foo and bar given their string names, after checking whether they are present in the object.
void doSomething (MyClass obj) {
for (String s : {"foo", "bar"}) {
if (object_contains (obj, s)) {
Object value = get_value_by_name (obj, s);
// ...
}
}
}
Given that "foo" and "bar" are given as strings, how can I write object_contains and get_value_by_name?

How to pass a method as an argument to a different method?

I have a method let's say in ClassA. I want to pass a method from ClassB as an argument to that method in ClassA. In this case I want to pass the getCode method from ClassB. I don't have any instance of ClassB and I'd like to achieve this without having to create one.
I've tried using simple method reference, but it does not work this way.
I don't want to make getCode a static method either.
public class ClassA {
public void validate() {
Validation validation = new Validation(ClassB::getCode, code);
//...
}
}
My final goal is to have a RequestValidator class to which add validations, each validation will be created with a specific method and a string in its constructor, in this case getCode from classB and code. Please note I only want one instance of RequestValidator. Something like this:
RequestValidator validator = new RequestValidator<>()
.addValidation(new Validation(ClassB::getCode, code))
.addValidation(new Validation(ClassB::getName, name));
getCode needs to be a static function, and the syntax would be ClassB.getCode. You would need ClassB to be imported into ClassA.
See:
Calling static method from another java class
Your use of a method reference will work just fine as long as you define the method arguments properly. You haven't given a lot of information, so I'm going to make some assumptions here. Please correct me if this isn't what you had in mind:
public class B {
public static String getCode() {
return "foobar"; // Replace with your own functionality
}
}
public class Validation {
Validation(Supplier<String> supplier, String code) {
String suppliedCode = supplier.get();
// Do your validation logic
}
}
public static void validate() {
Validation validation = new Validation(ClassB::getCode, code);
}
But this frankly feels like overkill. Why can't you just make your Validation constructor take two String arguments (or whatever types you happen to be using), and then do this?
public static void validate() {
Validation validation = new Validation(ClassB.getCode(), code);
}
Do you have a legitimate need to pass in a method reference instead of simply passing in the return value from the method call?

How can I handle a POJO like a bean?

How can I access a simple java object as a bean?
For example:
class Simple {
private String foo;
String getFoo() {
return foo;
}
private void setFoo( String foo ) {
this.foo = foo;
}
}
Now I want to use this object like this:
Simple simple = new Simple();
simple.setFoo( "hello" );
checkSettings( simple );
So I'm looking for the implementation of the method checkSettings( Object obj ):
public boolean checkSettings( Object obj ) {
// pseudocode here
Bean bean = new Bean( obj );
if( "hello".equals( bean.getAttribute( "foo" ) ) {
return true;
}
return false;
}
The java language contains a package called java.beans which sounds like it could help me. But I don't find a good starting point.
Any hints?
I think the functionality you're looking for resembles the one from the BeanUtils class of apache-commons:
http://commons.apache.org/beanutils/
Take a look at the getProperty() method of BeanUtils.
java.beans.Introspector.getBeanInfo yields an object implementing java.beans.BeanInfo, which in turn can be used to get PropertyDescriptors and MethodDescriptors (via its getPropertyDescriptors- and getMethodDescriptors-methods), which in turn can be used to get the information you actually want.
It is not really less effort than using reflection.
As stated in the question comments above I'm still not sure what you want, but it sort of sounds like you want to wrap an object gets & sets to an interface with a getAttribute. This is not what I think of as a "bean".
So you have an interface:
interface Thingie {
Object getAttribute(String attribute);
}
You would have to write an implementation of that that uses reflection.
class Thingie {
Object wrapped;
public Object getAttribute(String attribute) throws Exception {
Method[] methods = wrapped.getClass().getMethods();
for(Method m : methods) {
if (m.getName().equalsIgnoreCase("get"+attribute)) {
return m.invoke(wrapped);
}
}
}
}

Categories

Resources