I am getting HTTP Status 500 - Could not write JSON: could not initialize proxy - no Session
error on postman
window. Tried adding @JsonIgnore
; but not successful. Not able to understand why is this happening. Below is code:
config class
package com.wpits.acf.core.config;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.mobile.device.LiteDeviceResolver;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.wpits.acf.core.security.AppSecurityConfig;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.hibernate.HikariConfigurationUtil;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages=
{
"com.wpits.acf.core.repository"
})
@PropertySources({
@PropertySource("classpath:hibernate.properties"),
@PropertySource("classpath:application.properties")
})
@EnableWebMvc
@ComponentScan(basePackages={
"com.wpits.acf.core.exception",
"com.wpits.acf.core.security",
"com.wpits.acf.core.beans",
"com.wpits.acf.core.security.filter",
"com.wpits.acf.product.service",
"com.wpits.acf.product.controller",
"com.wpits.acf.*.service",
"com.wpits.acf.*.resourceapi",
"com.wpits.acf.*.beans"
})
@Import(AppSecurityConfig.class)
public class AppConfig extends WebMvcConfigurerAdapter{
private final static int maxUploadSizeInMb = 5 * 1024 * 1024;
@Autowired
private Environment env;
@Bean(name="dataSourceProp")
public Properties dataSourceProperties() {
Properties props=new Properties();
props.setProperty("javax.persistence.provider", env.getRequiredProperty("javax.persistence.provider"));
props.setProperty("hibernate.ddl-auto", env.getRequiredProperty("hibernate.ddl-auto"));
props.setProperty("hibernate.hikari.dataSourceClassName", env.getRequiredProperty("hibernate.hikari.dataSourceClassName"));
props.setProperty("hibernate.hikari.dataSource.url", env.getRequiredProperty("hibernate.hikari.dataSource.url"));
props.setProperty("hibernate.hikari.dataSource.user", env.getRequiredProperty("hibernate.hikari.dataSource.user"));
props.setProperty("hibernate.hikari.dataSource.password", env.getRequiredProperty("hibernate.hikari.dataSource.password"));
props.setProperty("hibernate.hikari.minimumIdle", env.getRequiredProperty("hibernate.hikari.minimumIdle"));
props.setProperty("hibernate.hikari.maximumPoolSize", env.getRequiredProperty("hibernate.hikari.maximumPoolSize"));
props.setProperty("hibernate.hikari.idleTimeout", env.getRequiredProperty("hibernate.hikari.idleTimeout"));
props.setProperty("hibernate.connection.handling_mode", env.getRequiredProperty("hibernate.connection.handling_mode"));
props.setProperty("hibernate.connection.provider_class", env.getRequiredProperty("hibernate.connection.provider_class"));
return props;
}
@Bean(name="dataSource",destroyMethod="close")
public DataSource configureDataSource(@Qualifier("dataSourceProp")Properties properties) {
HikariConfig hikariConfig=HikariConfigurationUtil.loadConfiguration(properties);
return new HikariDataSource(hikariConfig);
}
@Bean(name="entityManagerFactory",destroyMethod="close")
public EntityManagerFactory configureEntityManagerFactory(DataSource dataSource,@Qualifier("dataSourceProp")Properties jpaProperties){
LocalContainerEntityManagerFactoryBean factoryBean=new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setPackagesToScan("com.wpits.acf.core.model");
factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
factoryBean.afterPropertiesSet();
return factoryBean.getObject();
}
@Bean("transactionManager")
public JpaTransactionManager configureTransactionManager(EntityManagerFactory bean) {
JpaTransactionManager txm = new JpaTransactionManager(bean);
return txm;
}
@Bean
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver cmr = new CommonsMultipartResolver();
cmr.setMaxUploadSize(maxUploadSizeInMb * 2);
cmr.setMaxUploadSizePerFile(maxUploadSizeInMb); //bytes
return cmr;
}
@Bean
public LiteDeviceResolver deviceResolver(){
return new LiteDeviceResolver();
}
}
Product Controller
package com.wpits.acf.product.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.wpits.acf.core.model.Product;
import com.wpits.acf.product.service.ProductService;
@RestController
public class ProductController {
@Autowired
private ProductService productService;
@GetMapping(value = "productID/{productId}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Product> getProductById(@PathVariable(value="productId",required=true)int productId) {
System.out.println("reached here");
Product product=null;
product = productService.getProductById(productId);
if(product!=null) {
return new ResponseEntity<Product>(product,HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
Product Service
package com.wpits.acf.product.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.wpits.acf.core.model.Product;
import com.wpits.acf.core.repository.ProductRepository;
@Service
public class ProductService {
private final ProductRepository productRepository;
@Autowired
public ProductService(ProductRepository productRepository) {
this.productRepository = productRepository;
}
public Product getProductById(int id) {
return productRepository.findOne(id);
}
}
Product Entity class
package com.wpits.acf.core.model;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.Proxy;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@Entity
@Table(name = "product", catalog = "anti_counter_feit")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Product implements java.io.Serializable {
private static final long serialVersionUID = 4378274718614532475L;
private Integer id;
@JsonIgnore
private ProductCategory productCategory;
private UserMaster userMaster;
private String productName;
private String productDealerId;
private Date createdOn;
@JsonIgnore
private Set<Voucher> vouchers = new HashSet<Voucher>(0);
public Product() {
}
public Product(ProductCategory productCategory, UserMaster userMaster, String productName, Date createdOn) {
this.productCategory = productCategory;
this.userMaster = userMaster;
this.productName = productName;
this.createdOn = createdOn;
}
public Product(ProductCategory productCategory, UserMaster userMaster, String productName, String productDealerId,
Date createdOn, Set<Voucher> vouchers) {
this.productCategory = productCategory;
this.userMaster = userMaster;
this.productName = productName;
this.productDealerId = productDealerId;
this.createdOn = createdOn;
this.vouchers = vouchers;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "product_category_id", nullable = false)
public ProductCategory getProductCategory() {
return this.productCategory;
}
public void setProductCategory(ProductCategory productCategory) {
this.productCategory = productCategory;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
public UserMaster getUserMaster() {
return this.userMaster;
}
public void setUserMaster(UserMaster userMaster) {
this.userMaster = userMaster;
}
@Column(name = "product_name", nullable = false, length = 100)
public String getProductName() {
return this.productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
@Column(name = "product_dealer_id", length = 45)
public String getProductDealerId() {
return this.productDealerId;
}
public void setProductDealerId(String productDealerId) {
this.productDealerId = productDealerId;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_on", nullable = false, length = 19)
public Date getCreatedOn() {
return this.createdOn;
}
public void setCreatedOn(Date createdOn) {
this.createdOn = createdOn;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "product")
public Set<Voucher> getVouchers() {
return this.vouchers;
}
public void setVouchers(Set<Voucher> vouchers) {
this.vouchers = vouchers;
}
}
ProductCategory Entity class
package com.wpits.acf.core.model;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@Entity
@Table(name = "product_category", catalog = "anti_counter_feit")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class ProductCategory implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = -3766304364482482350L;
private Integer id;
private String productCategory;
@JsonIgnore
private Set<Product> products = new HashSet<Product>(0);
public ProductCategory() {
}
public ProductCategory(String productCategory) {
this.productCategory = productCategory;
}
public ProductCategory(String productCategory, Set<Product> products) {
this.productCategory = productCategory;
this.products = products;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "product_category", nullable = false, length = 100)
public String getProductCategory() {
return this.productCategory;
}
public void setProductCategory(String productCategory) {
this.productCategory = productCategory;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "productCategory")
public Set<Product> getProducts() {
return this.products;
}
public void setProducts(Set<Product> products) {
this.products = products;
}
}
UserMaster entity class
package com.wpits.acf.core.model;
import static javax.persistence.GenerationType.IDENTITY;
import java.util.Date;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;
import org.hibernate.annotations.Proxy;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@Entity
@Table(name = "user_master", catalog = "anti_counter_feit", uniqueConstraints = @UniqueConstraint(columnNames = "username"))
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class UserMaster implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = -2842909964796947777L;
private Integer id;
private Role role;
private String username;
private String password;
private String mobile;
private String email;
private String fullName;
private String companyName;
private String companyAddress;
private Date createdOn;
private Date updatedOn;
private boolean isActive;
@JsonIgnore
private Set<VoucherAccessHistory> voucherAccessHistories = null;
@JsonIgnore
private Set<Voucher> vouchers = null;
@JsonIgnore
private Set<Product> products = null;
public UserMaster() {
}
public UserMaster(Role role, String username, String password, String mobile, String email, String companyName,
String companyAddress, Date createdOn, Date updatedOn,boolean isActive) {
this.role = role;
this.username = username;
this.password = password;
this.mobile = mobile;
this.email = email;
this.companyName = companyName;
this.companyAddress = companyAddress;
this.createdOn = createdOn;
this.updatedOn = updatedOn;
this.isActive=isActive;
}
public UserMaster(Role role, String username, String password, String mobile, String email, String fullName,
String companyName, String companyAddress, Date createdOn, Date updatedOn) {
this.role = role;
this.username = username;
this.password = password;
this.mobile = mobile;
this.email = email;
this.fullName = fullName;
this.companyName = companyName;
this.companyAddress = companyAddress;
this.createdOn = createdOn;
this.updatedOn = updatedOn;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "role_id", nullable = false)
public Role getRole() {
return this.role;
}
public void setRole(Role role) {
this.role = role;
}
@Column(name = "username", unique = true, nullable = false, length = 45)
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name = "password", nullable = false, length = 100)
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name = "mobile", nullable = false, length = 15)
public String getMobile() {
return this.mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
@Column(name = "email", nullable = false, length = 45)
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
@Column(name = "full_name", length = 45)
public String getFullName() {
return this.fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
@Column(name = "company_name", nullable = false, length = 45)
public String getCompanyName() {
return this.companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
@Column(name = "company_address", nullable = false, length = 45)
public String getCompanyAddress() {
return this.companyAddress;
}
public void setCompanyAddress(String companyAddress) {
this.companyAddress = companyAddress;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_on", nullable = false, length = 19)
public Date getCreatedOn() {
return this.createdOn;
}
public void setCreatedOn(Date createdOn) {
this.createdOn = createdOn;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "updated_on", nullable = false, length = 19)
public Date getUpdatedOn() {
return this.updatedOn;
}
public void setUpdatedOn(Date updatedOn) {
this.updatedOn = updatedOn;
}
@Column(name = "is_active")
public boolean isActive() {
return isActive;
}
public void setActive(boolean isActive) {
this.isActive = isActive;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "userMaster")
public Set<VoucherAccessHistory> getVoucherAccessHistories() {
return this.voucherAccessHistories;
}
public void setVoucherAccessHistories(Set<VoucherAccessHistory> voucherAccessHistories) {
this.voucherAccessHistories = voucherAccessHistories;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "userMaster")
public Set<Voucher> getVouchers() {
return this.vouchers;
}
public void setVouchers(Set<Voucher> vouchers) {
this.vouchers = vouchers;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "userMaster")
public Set<Product> getProducts() {
return this.products;
}
public void setProducts(Set<Product> products) {
this.products = products;
}
}
When I start the server and hit controller through postman
, I get below output:
how can I resolve this issue?