Consume Json in REST post jersy - json

I am trying to consume json object in rest service,and convert it to local bean pojo jaxb class and use it for further processing.
Here is my webservice code:
#POST
#Path("login1")
#Consumes(MediaType.APPLICATION_JSON)
public String login1(LoginJSON data)
{
try
{
System.out.println("request received for user"+data.username);
System.out.println("pass: "+data.password);
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return "success";
}
Here is pojo class:
#XmlRootElement
public class LoginJSON
{
#XmlElement(required=true)
public String username;
#XmlElement(required=true)
public String password;
public LoginJSON()
{
super();
// TODO Auto-generated constructor stub
}
public LoginJSON(String username, String password)
{
super();
this.username = username;
this.password = password;
}
public String getUsername()
{
return username;
}
#XmlElement
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
#XmlElement
public void setPassword(String password)
{
this.password = password;
}
}
web.xml is :
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.myapp.webservices</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Webservice client is sending a json object with both user and password attribute.Error I am getting is :
INFO: No default constructor found on class
com.myapp.webservices.LoginJSON
java.lang.NoSuchMethodException: com.myapp.webservices.LoginJSON.<init>()
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getDeclaredConstructor(Unknown Source)
at com.sun.xml.internal.bind.v2.ClassFactory.create0(Unknown Source)
....
How do I resolve this any suggestions?

Remove the constructors used in the class LoginJSON
public LoginJSON()
{
super();
// TODO Auto-generated constructor stub
}
public LoginJSON(String username, String password)
{
super();
this.username = username;
this.password = password;
}
Let the class to use the default constructor, otherwise the json won't be parsed to LoginJSON.

The most simple library for processing JSON in java is org.json,
you don't need a POJO class:
#POST
#Path("login1")
#Consumes(MediaType.APPLICATION_JSON)
public String login1(LoginJSON data) {
JSONObject dataJSON = new JSONObject(data);
String username = dataJSON.get("username").toString();
String password = dataJSON.get("password").toString();
//...
return "success";
}

Related

What's the way to make controller if I want to pass this json code

I'd like to ask you how my rest controller should look like (in springboot) if I want to pass JSON like this
user: {email: "c", password: "c", username: "c"}
btw JSON which looks like this works fine:
{email: "c", password: "c", username: "c"}
so I think that it depends on 'user' word in JSON, but the problem is that my front-end sends all requests like this so better way would be to make this operable in backend.
because one my actual which looks like:
#PostMapping("/users")
public void register(#Valid #RequestBody ApplicationUserEntity newUser){
registerService.registerNewUser(newUser);
}
isn`t acutally working.
Here is the ApplicationUserEntity class:
#Entity
#Data
#Table(name = "users")
public class ApplicationUserEntity implements UserDetails {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#JsonIgnore
private Long userId;
#JsonProperty("email")
private String email;
#JsonProperty("username")
private String username;
#JsonProperty("password")
private String password;
public ApplicationUserEntity() {
}
public ApplicationUserEntity(String email, String username, String password) {
this.email = email;
this.username = username;
this.password = password;
}
#Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return Collections.singleton(new SimpleGrantedAuthority("ROLE_USER"));
}
#Override
public String getPassword() {
return password;
}
#Override
public String getUsername() {
return username;
}
#Override
public boolean isAccountNonExpired() {
return true;
}
#Override
public boolean isAccountNonLocked() {
return false;
}
#Override
public boolean isCredentialsNonExpired() {
return true;
}
#Override
public boolean isEnabled() {
return true;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
Your request body can be the following object:
public class Request {
private ApplicationUserEntity user;
// getters, setters ...
}
Its field is the Entity object that you have created. In this case your controller method would look like this:
#PostMapping("/users")
public void register(#Valid #RequestBody Request newUser){
registerService.registerNewUser(newUser);
}
The JSON request in this case would be:
{
user: {
// fields of the ApplicationUserEntity
}
}
Note: It is always recommended that you use DTO objects as requests and response objects. So in this case you would rather have a DTO object that contains the email, username, password field, and put this object as a field in the Request class.

Why am I getting InaccessibleObjectException when writing to JSON file

I'm using Gson to manage my Json file. I have a User class which looks like this:
public class User {
private StringProperty username;
private StringProperty password;
...
public User() {}
public User(String user, String pass, ...) {
this.username = new SimpleStringProperty(user);
this.password = new SimpleStringProperty(pass);
...
}
public String getUsername() {
return username.get();
}
public void setUsername(String username) {
this.username.set(username);;
}
...
}
And this is how I add a User to the Json file
public static boolean addUser(User user) throws IOException{
String users = new String(Files.readAllBytes(Paths.get("users.json")));
List<User> userList = getUsers(users);
if (userList.contains(user)) return false;
userList.add(user);
Writer writer = new FileWriter("users.json");
Gson gson = new GsonBuilder().create();
gson.toJson(userList, writer);
writer.close();
return true;
}
gson.toJson(userList, writer) is throwing this error:
Exception in thread "JavaFX Application Thread" java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.lang.Object javafx.beans.property.SimpleStringProperty.bean accessible: module javafx.base does not "opens javafx.beans.property" to unnamed module #4bf59938
I know it has something to do with the StringProperty attributes, but I don't know what's wrong.

Save Raw JSON as string in the database

How can I save Raw Json as String in the MsSql db with the POST request - using Jackson ObjectMapper to convert the string to Json but not able to change raw json into string?
{
"id": 1,
"someName":"someName",
"json": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossTerm": "Standard Generalized Markup Language"
}
},
"anotherjson":{
"name":"someone",
"age": 121
},
"somedate":"03-11-2019.00:00:00"
}
How can I save this save json as integer, varchar, string, string, date column in the db?
1,someName, "{"title": "example glossary","GlossDiv": {"title": "S","GlossTerm": "Standard Generalized Markup Language"}","{"name":"someone","age": 121}", 03-11-2019.00:00:00.
** Update **
For simplicity here is the simple json
{
"id":1,
"jsonObjectHolder":{
"name": "Name",
"age" : 404
}}
Controller:
#PostMapping("/postJson")
public void postJson(#RequestBody TryJson tryJson) {
tryJsonService.postJson(tryJson);
}
Service:
public void postJson(TryJson tryJson) {
tryJsonRepository.save(tryJson);
}
Repo:
public interface TryJsonRepository extends CrudRepository<TryJson, Integer> {
}
Model:
#Entity
#Table(name = "TryJson")
public class TryJson {
#Id
#Column(name = "id")
private Integer id;
#JsonIgnore
#Column(name = "json_column")
private String jsonColumn;
#Transient
private JsonNode jsonObjectHolder;
public TryJson() {
}
public TryJson(Integer id, String jsonColumn) {
this.id = id;
this.jsonColumn = jsonColumn;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public JsonNode getJsonObjectHolder() {
return jsonObjectHolder;
}
public void setJsonObjectHolder(JsonNode jsonObjectHolder) {
this.jsonObjectHolder = jsonObjectHolder;
}
public String getJsonColumn() {
return this.jsonObjectHolder.toString();
}
public void setJsonColumn(String jsonColumn) throws IOException {
ObjectMapper mapper = new ObjectMapper();
this.jsonObjectHolder = mapper.readTree(jsonColumn);
}
#Override
public String toString() {
return String.format("TryJson [Id=%s, JsonColumn=%s, jsonObjectHolder=%s]", id, jsonColumn, jsonObjectHolder);
}
}
http://localhost:8080/api/postJson
ID JSON_COLUMN
1 null
Not sure what I am missing here. I do get jsonObjectHolder populated during the debugging but then still I get NULL
TryJson [Id=1, JsonColumn=null, jsonObjectHolder={"name":"Name","age":404}]
Update 2
I am getting null pointer exception.
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: Exception occurred inside getter of com.example.tryjson.tryjson.model.TryJson.jsonColumn; nested exception is org.hibernate.PropertyAccessException: Exception occurred inside getter of com.example.tryjson.tryjson.model.TryJson.jsonColumn] with root cause
java.lang.NullPointerException: null
at com.example.tryjson.tryjson.model.TryJson.getJsonColumn(TryJson.java:52) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_192]
Here is my new model
#Entity
#Table(name = "TryJson")
public class TryJson {
private Integer id;
#Transient
private JsonNode jsonObjectHolder;
public TryJson() {
}
public TryJson(Integer id, JsonNode jsonObjectHolder) {
this.id = id;
this.jsonObjectHolder = jsonObjectHolder;
}
#Id
#Column(name = "id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
#Transient
public JsonNode getJsonObjectHolder() {
return jsonObjectHolder;
}
public void setJsonObjectHolder(JsonNode jsonObjectHolder) {
this.jsonObjectHolder = jsonObjectHolder;
}
#Column(name = "json_column")
public String getJsonColumn() {
return this.jsonObjectHolder.toString();
}
public void setJsonColumn(String jsonColumn) throws IOException {
ObjectMapper mapper = new ObjectMapper();
this.jsonObjectHolder = mapper.readTree(jsonColumn);
}
}
You could define a JsonNode json property to hold the part you want to persist as text, then mark it as #Transient so JPA does not try to store it on database. However, jackson should be able to translate it back and forward to Json.
Then you can code getter/setter for JPA, so you translate from JsonNode to String back and forward. You define a getter getJsonString that translate JsonNode json to String. That one can be mapped to a table column, like 'json_string', then you define a setter where you receive the String from JPA and parse it to JsonNode that will be avaialable for jackson.
Do not forget to add #JsonIgnore to getJsonString so Jackson does not try to translate to json as jsonString.
#Entity
#Table(name = "request")
public class Request {
private Long id;
private String someName;
#Transient
private JsonNode json;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
#Column(name ="someName")
public String getSomeName() {
return name;
}
public void setSomeName(String name) {
this.name = name;
}
public void setId(Long id) {
this.id = id;
}
// Getter and setter for name
#Transient // This is for Jackson
public JsonNode getJson() {
return json;
}
public void setJson(JsonNode json) {
this.json = json;
}
#Column(name ="jsonString")
public String getJsonString() { // This is for JPA
return this.json.toString();
}
public void setJsonString(String jsonString) { // This is for JPA
// parse from String to JsonNode object
ObjectMapper mapper = new ObjectMapper();
try {
this.json = mapper.readTree(jsonString);
} catch (Exception e) {
e.printStackTrace();
}
}
}
UPDATE:
If you mark jsonColumn with #Column spring will use reflection to pull out the data with default initialization null, getJsonColumn translation will never be executed:
#JsonIgnore
#Column(name = "json_column")
private String jsonColumn;
You do not need a jsonColumn, just make sure you mark your setters with #Column, so spring uses gettets/setters to persist to database, when persisting, jpa will execute getJsonColumn, when reading, jpa will execute setJsonColumn and jsonNode will be translated back and forward to string:
#Entity
#Table(name = "TryJson") public class TryJson {
private Integer id;
#Transient
private JsonNode jsonObjectHolder;
public TryJson() {
}
public TryJson(Integer id, String jsonColumn) {
this.id = id;
this.jsonObjectHolder = // use mapper to create the jsonObject;
}
#Id
#Column(name = "id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public JsonNode getJsonObjectHolder() {
return jsonObjectHolder;
}
public void setJsonObjectHolder(JsonNode jsonObjectHolder) {
this.jsonObjectHolder = jsonObjectHolder;
}
#Column(name = "json_column")
public String getJsonColumn() {
return this.jsonObjectHolder.toString();
}
public void setJsonColumn(String jsonColumn) throws IOException {
ObjectMapper mapper = new ObjectMapper();
this.jsonObjectHolder = mapper.readTree(jsonColumn);
}
#Override
public String toString() {
return String.format("TryJson [Id=%s, JsonColumn=%s, jsonObjectHolder=%s]", id, jsonColumn, jsonObjectHolder);
}
}
ObjectMapper mapper = new ObjectMapper();
TypeReference<List<User>> typeReference = new TypeReference<List<Your_Entity>>() {};
InputStream inputStream = TypeReference.class.getResourceAsStream("/bootstrap.json");
try {
List<Your_Entity> users = mapper.readValue(inputStream, typeReference);
log.info("Saving users...");
userService.saveAllUsers(users);
log.info(users.size() + " Users Saved...");
} catch (IOException e) {
log.error("Unable to save users: " + e.getMessage());
}

javafx tableview not able to fetch data while initializing

I'm trying to populate tableview at the time of loading. Please forgive if there is any mistake.
public class users {
public String username;
public String FullName;
public String password;
public String phone;
public String email;
public String doj;
public String city;
public String state;
public String address;
public ObservableList <ListEmply> emplylst = FXCollections.observableArrayList();
}
public class UserDetail {
#FXML
private ObservableList <ListEmply> emplylst;
#FXML
private TableView <ListEmply> tbl_employeeview;
#FXML
private TableColumn<Object, Object> employeename;
users User = new users();
Dbconnection dbcon = new Dbconnection();
Connection con;
PreparedStatement pst;
ResultSet rs;
public void showDetails(users User){
con = dbcon.geConnection();
try{
pst = con.prepareStatement("select room_no from room");
rs = pst.executeQuery();
while (rs.next()){
User.emplylst.add(new ListEmply(
rs.getString(1)
));
}
System.out.println(rs);
rs.close();
pst.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void usrdetails(){
tbl_employeeview.setItems(emplylst);
showDetails(User);
employeename.setCellValueFactory(new PropertyValueFactory<>("employeename"));
System.out.println(rs);
}
public void Initializable(URL url, ResourceBundle rb){
usrdetails();
}
}
ListEmply Class
public class ListEmply {
public String employeename;
public ListEmply(String employeename) {
super();
this.employeename = employeename;
}
public String getEmployeename() {
return employeename;
}
}
public void setEmployeename(String employeename) {
this.employeename = employeename;
}
}
As described in the documentation, the controller method that is called to initialize the controller is called initialize(...), not Initializable(...):
public void initialize(URL url, ResourceBundle rb){
usrdetails();
}
As noted by #fabian in the comments, since you are not implementing the (legacy) interface Initializable and not using the parameters, you can omit the parameters from the method definition:
public void initialize(){
usrdetails();
}

JAXRS client - Deserialization Issue with Pojo

Helo Everyone. I am new to jersey and Jackson and finding it really difficult to deserialize a JSOn response from my REST service on client side. I am pretty sure that I am still not grasping the Object mapper and JSON provider APIs that well. Any help or guidance is appreciated in advance. Here is my source code.
Source Code
POJO Class
#XmlRootElement(name = "user")
public class User implements Serializable{
private String firstName;
private String lastName;
private String email;
private String userID;
public User() {
}
public User(String firstName, String lastName, String email, String userID) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.userID = userID;
}
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 String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
}
Client Code
package com.example.service.client;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.jackson.JacksonFeature;
import com.example.service.bean.User;
import com.example.service.client.mapper.MyMessageBodyReader;
import com.example.service.client.mapper.MyObjectMapperProvider;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
public class GetJSONResponse {
public static void main(String[] args) {
// TODO Auto-generated method stub
JacksonJaxbJsonProvider provider1 = new JacksonJaxbJsonProvider();
Client c = ClientBuilder.newClient()register(provider1);//.register(mapper);
WebTarget target = c.target("http://localhost:8080/RestfulWebserviceExample").path("/jaxrs/user/Nishit");
Response resp = target.request(MediaType.APPLICATION_JSON).get();
System.out.println(resp.getStatus());
String user1 = resp.readEntity(String.class);
System.out.println(user1);
User user = target.request(MediaType.APPLICATION_JSON).get(User.class);
System.out.println("User : " + user.getUserID());
}
}
`
The first 2 sysout generates an output as
200
{"user":{"firstName":"Nishit","lastName":"Ladha","email":"ladha#us.ibm.com","userID":"nishiz"}}
But when i tries to directly get the User object from response, I get an error as
Exception in thread "main" javax.ws.rs.client.ResponseProcessingException: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "user" (class com.example.service.bean.User), not marked as ignorable (4 known properties: "lastName", "firstName", "email", "userID"])
at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream#10163d6; line: 1, column: 10] (through reference chain: com.example.service.bean.User["user"])
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:806)
at org.glassfish.jersey.client.JerseyInvocation.access$700(JerseyInvocation.java:92)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:700)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:696)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:420)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:316)
at com.example.service.client.GetJSONResponse.main(GetJSONResponse.java:40)
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "user" (class com.example.service.bean.User), not marked as ignorable (4 known properties: "lastName", "firstName", "email", "userID"])
It will be really kind if anyone of you can guide me how to resolve this.
I am not using Maven as I first wanted to try without Maven
I am not sure why my rest service is wrapping the response. Here is the code :
Service Method
#GET
#Path("/{username}")
#Produces(MediaType.APPLICATION_JSON)
public User helloWorld(#PathParam("username") String name){
User user = new User();
user.setFirstName("Nishit");
user.setLastName("Ladha");
user.setUserID("nishiz");
user.setEmail("ladha#us.ibm.com");
return user;
}
Web.xml##
<servlet>
<description>JAX-RS Tools Generated - Do not modify</description>
<servlet-name>JAX-RS Servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
com.example.service,
com.fasterxml.jackson.jaxrs.json
</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
Man, look, you are trying to bind unexistent field User.
If you want to properly parse this json
{"user":{"firstName":"Nishit","lastName":"Ladha","email":"ladha#us.ibm.com","userID":"nishiz"}}
You need to have similar to this class
public class UserWrapper implements Serializable{
private User user;
// Constructors
// Getters, and setters
// HashCode and equals
}
Then this client code will work:
public class GetJSONResponse {
public static void main(String[] args) {
// TODO Auto-generated method stub
JacksonJaxbJsonProvider provider1 = new JacksonJaxbJsonProvider();
Client c = ClientBuilder.newClient()register(provider1);//.register(mapper);
WebTarget target = c.target("http://localhost:8080/RestfulWebserviceExample").path("/jaxrs/user/Nishit");
Response resp = target.request(MediaType.APPLICATION_JSON).get();
System.out.println(resp.getStatus());
String user1 = resp.readEntity(String.class);
System.out.println(user1);
UserWrapper userWrapper = target.request(MediaType.APPLICATION_JSON).get(UserWrapper.class);
}
}
If you have any questions - just ask.Hope your code will work.