How to handle the following case with katharsis? - java

I have a model and repository. Model has getter and setter values which has to be added in the response. My model is like follows
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.katharsis.resource.annotations.JsonApiId;
import io.katharsis.resource.annotations.JsonApiResource;
#JsonApiResource(type="employee") //no i18n
public class Employee {
#JsonApiId
private String name;
private int emp_id;
private String dob;
private String profile_url;
private String status_message;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getEmp_id() {
return this.emp_id;
}
public void setEmp_id(int empid) {
this.emp_id = empid;
}
public String getDob() {
return this.dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getProfile_url() {
return this.profile_url;
}
public void setProfile_url(String url) {
this.profile_url = url;
}
public String getStatus_message() {
return this.status_message;
}
public void setStatus_message(String message) {
this.status_message = message;
}
}
Here the variable status_message represents my api status. The api response should be like
When the database has the employee with inputted id
{name : "rajasuba", emp_id : "123", dob : "March301993", profile_url : "https:", status_message : "success"}
When the employee left the organization the response should be like
{name : "rajasuba", emp_id : "567", status_messsage : "fired"}
When there is no such employee then my response should be like
{status_message : "Invalid employeed id"}
But for all the above cases i am getting all the attribute values. How can i ignore the attribute value (like #JsonIgnore) selectively for a particular scenario?

I don't know what version of katharsis you're using, but in 2.8.2 it's supported

Related

Api endpoint that edits one element in list

I am currently building a rest api that lets the user enter a recipe and describe it. I am using spring-boot as backend and angularjs as frontend.
This is springboot recipe file
package com.example.springboot;
import com.example.springboot.Recipe;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
#Entity // This tells Hibernate to make a table out of this class
public class Recipe {
public Recipe(){
}
public Recipe(Integer id, String name, String description, String type, Integer preptime, Integer cooktime, String content, Integer difficulty){
this.id = id;
this.name = name;
this.description = description;
this.type = type;
this.preptime = preptimee;
this.cooktime = cooktime;
this.content = content;
this.difficulty = difficulty;
}
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String name;
private String description;
private String type;
private Integer preptime;
#Column(columnDefinition = "TEXT")
private String content;
private Integer difficulty;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Integer getDifficulty() {
return difficulty;
}
public void setDifficulty(Integer difficulty) {
this.difficulty = difficulty;
}
public Integer getpreptime {
return preptime;
}
public void setpreptime(Integer preptime) {
this.preptime = preptime;
}
}
I created an Endpoint where the user can edit the whole recipe. The user can edit the name , description, content and so on in the recipes/edit/{id} endpoint.
The Endpoint looks like this.
#PutMapping("/recipes/edit/{id}")
void updateRecipe(#PathVariable int id, #RequestBody Recipe recipe ) {
System.out.println("entering");
Recipe recipe_ = recipeRepository.findById(id).get();
recipe_.setName(recipe.getName());
recipe_.setDescription(recipe.getDescription());
recipe_.setType(recipe.getType());
recipe_.setpreptime(recipe.getpreptime());
recipe_.setContent(recipe.getContent());
System.out.println("entering " + recipe.getTitle());
System.out.println("entering" + recipe.getType());
System.out.println("entering" + recipe.getDescription());
System.out.println("adding");
recipeRepository.save(recipe_);
}
Now I just want to create an Endpoint which only serves the purpose for renaming the name of the recipe. This putmapping should accept a list as its input then only rename the name of the recipe.
#PutMapping("/recipes/rename")
public List<Recipe> {
System.out.println("entering renaming");
// recipe_.setName(recipe.getName()); ?
}
I don't know how I can implement this. This is what I have come up with so far. An endpoint which takes a list as a parameter.
This is the service.ts file that updates the Recipes in the edit function
service.ts:
updateRecipe (id: number, recipe: any): Observable<any > {
const url = `${this.usersUrl}/edit/${id}`;
return this.http.put(url ,recipe);
}
where the updateRecipe gets called:
save(): void {
const id = +this.route.snapshot.paramMap.get('name');
this.recipeService.updateRecipe2(id, this.recipes)
.subscribe(() => this.gotoUserList());
}
This implementation work , I don't know how I can get it work or how I can rewrite the functions so that it can update only the name of the recipe and not the whole file.
Could someone help me?
Your update the name method should look like that:
#PutMapping("...{id}")
public void updateName(#PathVariable Integer id, #RequestParam String name){
Recipe recipe = repository.findById(id).orElseThrow(...);
recipe.setName(name);
}
if you want to rename list of recipes
public void renameRecipes(String oldName, String newName){
repository.findByName(oldName)
.forEach(r -> r.setName(newName));
}
#PutMapping("recipes/rename")
public void updateNames(#PequestParam String oldName, #RequestParam String newName){
renameRecipes(oldName, newName);
}
Try that.

JSON parse error: Cannot deserialize instance of.. out of START_ARRAY token

I know there are a few questions on stackoverflow regarding this problem. But I have have been spending hours trying to resolve this error without any success.
I am using the mysql database to store the values.
I keep on getting the error message from the
com.example.springboot.Recipe file.
This is springboot recipe file
package com.example.springboot;
import com.example.springboot.Recipe;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
#Entity // This tells Hibernate to make a table out of this class
public class Recipe {
public Recipe(){
}
public Recipe(Integer id, String name, String description, String type, Integer preptime, Integer cooktime, String content, Integer difficulty){
this.id = id;
this.name = name;
this.description = description;
this.type = type;
this.preptime = preptimee;
this.cooktime = cooktime;
this.content = content;
this.difficulty = difficulty;
}
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String name;
private String description;
private String type;
private Integer preptime;
private Integer cooktime;
#Column(columnDefinition = "TEXT")
private String content;
private Integer difficulty;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return name;
}
public void setTitle(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Integer getDifficulty() {
return difficulty;
}
public void setDifficulty(Integer difficulty) {
this.difficulty = difficulty;
}
public Integer getCookingtime() {
return cooktime;
}
public void setCookingtimeime(Integer cooktime) {
this.cooktime = cooktime;
}
public Integer getPreparationtime() {
return preptime;
}
public void setPreparationtime(Integer preptime) {
this.preptime = preptime;
}
}
Main Controller:
#PutMapping("/recipes/edit/{id}")
void updateRecipe2(#PathVariable int id, #RequestBody Recipe recipe ) {
Recipe recipe_ = recipeRepository.findById(id).get();
recipe_.setTitle(recipe.getTitle());
System.out.println("sss " + recipe.getname());
System.out.println("change");
recipeRepository.save(recipe_);
}
service.ts:
updateRecipe2 (id: number, recipe: any): Observable<any > {
const url = `${this.usersUrl}/edit/${id}`;
return this.http.put(url ,recipe);
}
where the updateRecipe2 gets called:
save(): void {
const id = +this.route.snapshot.paramMap.get('name');
this.recipeService.updateRecipe2(id, this.recipes)
.subscribe(() => this.gotoUserList());
}
as soon as the user clicks save this functions saves the changes made.
I hope the code snippets that I provided are enough to help solve the problem.
Thank you in advance.
I am building a rest api with spring boot and I am using angularjs as it's frontend. I am pretty new to web-development.
You are sending a list of recipes to an api endpoint that expects a single recipe object.
Your options are:
Send only one recipe object at a time, for example:
this.recipeService.updateRecipe2(id, this.recipes[0])
OR: create a new API endpoint to accept a list of recipes, to edit them in "batch"
#PutMapping("/recipes/edit")
void updateRecipes(#RequestBody List<Recipe> recipe ) {
my Example:
Use:
#PostMapping
Code:
public void setTransacciones(List<Transacciones> transacciones) {
this.transacciones = transacciones;
}
CodeBean:
public class Transacciones {
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
private String text;
}
Post(raw):
{
"transacciones" : [ {"text" : "1"}, {"text" : "2"} ]
}
Result:
{
"transacciones": [
{
"transaccionId": 2,
"text": "1"
},
{
"transaccionId": 3,
"text": "2"
}
]
}
BINGO!!

How to specify a default assembler in seedstack?

I'm using the 19.11 version of seedstack, and I want to use the FluentAssembler assembler to convert an aggregate List into a DTO List.
I'm getting the following error when I call the fluentAssembler.assemble method :
org.seedstack.business.internal.BusinessException: [BUSINESS] Unable to find assembler
Description
-----------
No assembler was found to assemble 'com.inetpsa.svr.domain.model.customer.Customer(Customer.java:1)' to
'com.inetpsa.svr.interfaces.rest.customer.CustomerRepresentation(CustomerRepresentation.java:1)'.
Fix
---
Make sure that an assembler without qualifier exists. If you want to use a qualified assembler (like a default
assembler), specify its qualifier.
I don't know howto specify the qualifier, I'd like to use a default model mapper...
Here is The Resource code :
#Path("customers")
public class CustomerResource {
#Inject
private FluentAssembler fluentAssembler;
#GET
#Produces(MediaType.APPLICATION_JSON)
public List<CustomerRepresentation> listAllCustomers() {
List<Customer> customerList = fetchAllCustomers();
return fluentAssembler.assemble(customerList).toListOf(CustomerRepresentation.class);
}
/**
* Test method - Should be replaced by a repository
* #return List<Customer> all customers
*/
private List<Customer> fetchAllCustomers(){
List<Customer> customerList = new ArrayList<>();
customerList.add(buildCustomer("005","Edward Teach","edward.teach#pirates.org"));
customerList.add(buildCustomer("006","Olivier Levasseur","olivier.levasseur#pirates.org"));
customerList.add(buildCustomer("007","James Bond","james.bond#mi6.uk"));
return customerList;
}
private Customer buildCustomer(String id, String name, String mail){
Customer result = new Customer(id);
result.updateNameAndMail(name, mail);
return result;
}
}
The aggregate :
public class Customer extends BaseAggregateRoot<String> {
#Identity
private String identifier;
private String name;
private String mail;
public Customer(String identifier){
this.identifier=identifier;
}
public void updateNameAndMail(String name, String mail){
if(StringUtils.isBlank(name)){
throw new IllegalArgumentException("Name can't be blank");
}
if(StringUtils.isBlank(mail)){
throw new IllegalArgumentException("Mail can't be blank");
}
this.name=name;
this.mail=mail;
}
public String getIdentifier() {
return identifier;
}
public String getName() {
return name;
}
public String getMail() {
return mail;
}
}
And the DTO :
#DtoOf(Customer.class)
public class CustomerRepresentation {
private String identifier;
private String name;
private String mail;
/**
* Required public no parameters constructor
*/
public CustomerRepresentation(){}
public CustomerRepresentation(String identifier, String name, String mail){
}
#AggregateId
public String getIdentifier() {
return identifier;
}
public String getMail() {
return mail;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setIdentifier(String identifier) {
this.identifier = identifier;
}
public void setMail(String mail) {
this.mail = mail;
}
}
FluentAssembler only takes care of matching a Dto with an Assembler, but does not provide a default implementation of an Assembler by itself.
You have 2 Options to provide a Default Assembler.
Build a class that implements Asselmber
Include an addon that provides that Default Assember for you (As stated on the docs)

Firebase DataSnapshot Not Filling Object

I have an object in a firebase database that I am trying to extract from a snapshot.
The JSON object from firebase is:
"-KoVHZ8YVll0RiI2GwKb" : {
"name" : "Name 1",
"phone_number" : "4443335555"
}
I am trying to safe it in an object that looks like this
public class Contact {
private String mName;
private String mPhoneNumber;
public Contact() {
mName = "";
mPhoneNumber = "";
}
public String getName() { return mName; }
public String getPhoneNumber() { return mPhoneNumber; }
public void setName(final String name) { mName = name; }
public void setPhoneNumber(final String phoneNumber) { mPhoneNumber = phoneNumber; }
}
I am calling
Contact contact = snapshot.getValue(Contact.class);
The contact object only has the name populated and not the phone number. The documentation just states that there must be public getters and an empty constructor in order for this to work. My guess is something is wrong with my naming convention anyone have any ideas?
EDIT
I am aware that I can extract the data out by doing this:
mName = (String) snapshot.child("nane").getValue();
mPhoneNumber = (String) snapshot.child("phone_number").getValue();
But then what is the point of creating the P.O.J.O.?
Make your Contact.java like this:
#IgnoreExtraProperties
public class Contact {
private String name;
private String phone_number;
public Contact() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone_number() {
return phone_number;
}
public void setPhone_number(String phone_number) {
this.phone_number = phone_number;
}
}

Parse json into multiple java pojos

I am writing a Restful webservice which would receive data in the below format.
{
"myOrder": {
"submitDate": "2015-04-16T02:52:01.406-04:00",
"supplier": "Amazon",
"orderName": "Wifi Router",
"submittedBy": "Gaurav Varma",
"price": {
"value": "2000",
"currency": "USD"
},
"address": {
"name": "My home",
"address": "Unknow island",
"city": "Mainland China",
"state": "Xinjiang",
"contact": {
"firstName": "Gaurav",
"lastName": "Varma",
"phone": "000-000-0000",
"email": "test#gv.com"
}
}
}
}
To read that data I am considering Jackson or GSON frameworks. The easiest way would be to use a Java POJO which has exactly the same structure as the json request. But for me the structure of Java POJOs is different. I have four different pojo as mentioned below :
Submitter.java
- SubmittedBy
- SubmitDate
Order.java
- Supplier
- OrderName
Price.java
- Value
- Currency
Address.java
- Name
- Address
- City
- State
Contact.java
- FirstName
- LastName
- Phone
- Email
Question : Is it a way to parse the json once into five different POJOs. May be some annotation based approach where we can map json attribute to respective pojo attribute? Any framework available for it?
Thanks in advance !
I'm currently using Jackson on my project. You have the option of annotating your POJO fields with #JsonProperty or #JsonUnwrapped. You would use #JsonUnwrapped on Order, for example, and then Order would have two fields (supplier and orderName) that use #JsonProperty.
See here for more details.
You could use eclipse link moxy for this. It uses JAXB style annotations for field to JSON/XML mapping.
Moxy is part of eclipse link.
Wikipedia:
EclipseLink is the open source Eclipse Persistence Services Project
from the Eclipse Foundation. The software provides an extensible
framework that allows Java developers to interact with various data
services, including databases, web services, Object XML mapping (OXM),
and Enterprise Information Systems (EIS).
So in your code you would use it like;
Model A:
#XmlElement(name="completed_in")
public float getCompletedIn() {
return completedIn;
}
Model B:
#XmlElement(name="created_at")
#XmlJavaTypeAdapter(DateAdapter.class)
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
#XmlElement(name="from_user")
public String getFromUser() {
return fromUser;
}
Json:
{
"completed_in":0.153,
{
"created_at":"Fri, 12 Aug 2011 01:14:57 +0000",
"from_user":"stackfeed",
you can use the composition design pattern and have an instance of each object in a wrapper class. Or you can try to parse the json into a map and write code to instantiate and set the variables as needed.
You could use Jackson; I think you need a POJO to wrapp the Order and Address like
class FullOrder {
Order order;
Address address;
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
With this you can easily use Jackson
String json; // your json here
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.readValue(json, FullOrder.class);
And that will parse the json into your pojo. Hope it helps you
The full structure
class Submitter {
private Date submittedBy;
private Date submitDate;
public Date getSubmittedBy() {
return SubmittedBy;
}
public void setSubmittedBy(Date submittedBy) {
SubmittedBy = submittedBy;
}
public Date getSubmitDate() {
return SubmitDate;
}
public void setSubmitDate(Date submitDate) {
SubmitDate = submitDate;
}
}
class Order {
private String supplier;
private String orderName;
private Price price;
private Submitter submitter;
public Price getPrice() {
return price;
}
public void setPrice(Price price) {
this.price = price;
}
public Submitter getSubmitter() {
return submitter;
}
public void setSubmitter(Submitter submitter) {
this.submitter = submitter;
}
public String getSupplier() {
return Supplier;
}
public void setSupplier(String supplier) {
Supplier = supplier;
}
public String getOrderName() {
return OrderName;
}
public void setOrderName(String orderName) {
OrderName = orderName;
}
}
class Price {
private int value;
private int currency;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int getCurrency() {
return currency;
}
public void setCurrency(int currency) {
this.currency = currency;
}
}
class Address {
private String name;
private String address;
private String city;
private String state;
private Contact contact;
public Contact getContact() {
return contact;
}
public void setContact(Contact contact) {
this.contact = contact;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
class Contact {
String firstName;
String lastName;
long phone;
String email;
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 long getPhone() {
return phone;
}
public void setPhone(long phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
class FullOrder {
Order myOrder;
Address address;
public Order getMyOrder() {
return order;
}
public void setMyOrder(Order order) {
this.order = order;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
This is structure of your json, you only need to copy it and use the Object mapper to parse the json to the pojo (FullOrder) that contains the other pojos and properties
String json; // your json here
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.readValue(json, FullOrder.class);
I figured out the solution approach. Posting for other users. The complete implementation is on my blog - http://javareferencegv.blogspot.com/2015/04/parse-json-into-multiple-java-pojos.html
So basically 3 points regarding solution approach:
We use Jackson annotation - #JsonIgnoreProperties. This would make
sure only those fields in Pojo are mapped to JSON attributes. So we
read the json twice, once mapping to Order.java and then to
Submitter.java. Both gets the correspondingly mapped fields.
We use Jackson annotation - #JsonProperty. This lets us map the exact JSON attribute to a field in POJO. The annotation makes sure different named attributes in JSON and POJO are mapped.
Jackson doesn't provide any annotation to perform #JsonWrapped (The vice-versa #JsonUnwrapped is available for serialization). Hence, we map Price as an attribute in Order.java.
The main class looks like this :
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonDeserializer {
public static void main(String[] args) {
try {
// ObjectMapper provides functionality for data binding between
ObjectMapper mapper = new ObjectMapper();
String jsonString = "{\"submitDate\":\"2015-04-16\",\"submittedBy\":\"Gaurav Varma\",\"supplier\":\"Amazon\",\"orderName\":\"This is my order\","
+ "\"price\": {\"value\": \"2000\",\"currency\": \"USD\"}"
+ "}";
System.out.println("JSON String: " + jsonString);
// Deserialize JSON to java format and write to specific POJOs
Submitter submitterObj = mapper.readValue(jsonString, Submitter.class);
Order orderObj = mapper.readValue(jsonString, Order.class);
Price priceObj = orderObj.getPrice();
System.out.println("submitterObj: " + submitterObj);
System.out.println("orderObj: " + orderObj);
System.out.println("priceObj: " + priceObj);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Categories

Resources