SonarQube issue resolve using #NotNull and #JsonProperty together - java

I'm working on sonarQube issues in my company, on previously developed module I've to modify variable names as a part of fix. So I've applied #JsonProperty but I can't remove #NotNull as well. So my problem is #JsonProperty doing it's job but not null validation failing (THROWING EXCEPTION). Please help me with solution should I apply both annotation's. We are using spring mvc, and restful call to this dto. For now I don't want to make any validation in controller.
public class CustomerImpl extends Customer {
#NotNull(message = "should not be null")
#JsonProperty("customer_Id")
private int customerId;
#NotNull(message = "should not be null")
#JsonProperty("first_name")
private String firstName;
#NotNull(message = "should not be null")
#JsonProperty("last_name")
private String lastName;
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}

Related

H2 database columns and values don't converge JAVA Spring

I am new one at java and spring framework and have this problem. I have class, which has fields, that should be columns in H2. It looks like this:
package com.bankapp.bankwebapplication.models;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
#Entity
public class PersonClient implements Client {
#Id
#Column(nullable = false, unique = true)
private Long id;
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
#Column(nullable = false)
private String firstName;
public String getFirstName() { return firstName; }
public void setFirstName(String firstName) { this.firstName = firstName; }
#Column(nullable = false)
private String lastName;
public String getLastName() { return lastName; }
public void setLastName(String lastName) { this.lastName = lastName; }
#Column(nullable = false)
private String address;
public String getAddress() { return address; }
public void setAddress(String address) { this.address = address; }
#Column
private String workPhone;
public String getWorkPhone() { return workPhone; }
public void setWorkPhone(String workPhone) { this.workPhone = workPhone; }
#Column
private String homePhone;
public String getHomePhone() { return homePhone; }
public void setHomePhone(String homePhone) { this.homePhone = homePhone; }
#Override
public void getDetails() {
}
}
Also, I have data.sql file that inserts 1 value into that table:
INSERT INTO person_client VALUES (1, 'firstName', 'lastName', 'paper street', '+123123', '+321321')
So, the problem is that it looks like this:
Why? And how can I fix that?
Always specify the target columns in INSERT statements:
INSERT INTO person_client
(id, first_name, last_name, address, home_phone, work_phone)
VALUES
(1, 'firstName', 'lastName', 'paper street', '+123123', '+321321')
If you don't specify the target columns, the values are matched by position and apparently the columns are created in a different order than you think they are.
agree with #a_horse_with_no_name, if you not specify column names it will insert based on the position/index. And all your java variables are in string that is the reason it does't throw any classcast exception.

How can I exclude a field from the schema when generating schemas in Jackon?

I'm using Jackson's JsonSchemaGenerator to generate schemas for my beans. One of these beans have a getter that I would like to exclude from the schema generation (or alternatively, mark is as an "object" - not any).
How would I go about generating a schema but forcing the address property to be either excluded or any object?
public static class Person {
private final String firstName;
private final String lastName;
private final Address address;
public Person(
#JsonProperty("first_name") String firstName,
#JsonProperty("last_name") String lastName,
#JsonProperty("address") Address address) {
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
}
#JsonProperty("first_name")
public String getFirstName() {
return firstName;
}
#JsonProperty("last_name")
public String getLastName() {
return lastName;
}
#JsonProperty("address")
public Address getAddress() {
return address;
}
}
I haven't found a way to achieve this using Jackson.

Can I use both #data and #builder annotations in one class Lombok?

The reason I want to use is because I want to generate setters in this format
For the class
public class Person {
private String firstName;
private String lastName;
}
public Person setFirstName(String firstName) {
this.firstName = firstName;
return this;
}
public Person setLastName(String lastName) {
this.lastName = lastName;
return this;
}
Instead of the #Data generated setters
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
Or is there any other way? Please let me know.
For it, you should use #Accessors(chain = true). For example:
#Accessors(chain = true)
#Setter
public class Person {
private String firstName;
private String lastName;
}
Vanilla java representation:
public class Person {
private String firstName;
private String lastName;
public Person setFirstName(String firstName) {
this.firstName = firstName;
return this;
}
public Person setLastName(String lastName) {
this.lastName = lastName;
return this;
}
}

Endpoints method not working, why?

When I test my google endpoints API in the api explorer, the saveProfile method throws the 503 Service Unavailable error, but all other methods work fine.
Here is the saveProfile method in my endpoint:
#ApiMethod(name = "saveProfile")
public Profile saveProfile(final ProfileForm profileForm) {
String firstName = profileForm.getFirstName();
String lastName = profileForm.getLastName();
String email = profileForm.getEmail();
Profile profile = new Profile("124234132", email, firstName, lastName);
ofy().save().entity(profile).now();
return profile;
}
Here is the Profile entity class:
#Entity
public class Profile {
#Id
private String userId;
private String email;
private String firstName;
private String lastName;
public Profile(String userId, String email, String firstName, String lastName) {
this.userId = userId;
this.email = email;
this.firstName = firstName;
this.lastName = lastName;
}
}
Here is the profileForm class:
public class ProfileForm {
private String firstName;
private String lastName;
private String email;
public ProfileForm() {}
public ProfileForm(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getEmail() {
return email;
}
}
I have registered the entity and have set the API_EXPLORER_CLIENT_ID as well as the web and android client IDs.
Does somebody understand how to fix this so that the method just returns the profile object as it should?
In order for Profile to be serializable you need to define getters and setters for each of its fields (ex. getUserId(), setUserId(string Id)).
An Objectify entity must also include a no-arg constructor: https://github.com/objectify/objectify/wiki/Entities
Let me know if that fixes the error.

Class mypack.pages.User has been transformed and may not be directly instantiated

I am trying to display a grid with tapestry based on this Tutorial, but i'm getting this error Class mypack.pages.User has been transformed and may not be directly instantiated
those are my classes User
public class User {
#NonVisual
private long id;
private String firstName;
private String lastName;
private int age;
public long getId() { return id; }
public void setId(long id) { this.id = id; }
public String getFirstName() { return firstName; }
public void setFirstName(String firstName) { this.firstName = firstName; }
public String getLastName() { return lastName; }
public void setLastName(String lastName) { this.lastName = lastName; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public User(long id, String firstName, String lastName, int age) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
}
Bellilpage.java
public class Bellilpage {
#Property
private User user;
public List<User> getUsers() {
List<User> dd= new ArrayList<User>();
for(int x=0;x<1;x++)
{
Random rand = new Random();
long d= rand.nextInt(50);
User myuser = new User(d, "Name N° "+d, "lastName N "+d, (int) (d+15));
dd.add(myuser);
}
return dd; }
}
and finally this is how i try to display the grid in the web page
Bellilpage.tml
<html t:type="layout" title="tapestrythetest Index"
t:sidebarTitle="Framework Version"
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"
xmlns:p="tapestry:parameter">
<!-- A Zone is a component that can be updated in place, triggered by other components. -->
<t:zone t:id="zone">
<h1>List Users</h1>
<t:grid source="users" row="user">
<p:lastNameCell>
${user.lastname}
</p:lastNameCell>
</t:grid>
</t:zone>
<p:sidebar>
</p:sidebar>
</html>
Why am i gtting this error when i open Bellilpage.tml?
You are getting the error because mypack.pages is a T5 controlled package. Move your User class to a different package, e.g. to mypack.entities. More info at Component Classes, specifically the Component Packages section.

Categories