org.springframework.web.HttpMediaTypeNotSupportedException

时间:2019-01-17 06:05:00

标签: spring-boot

 when i am trying to get values by id ..i got error like this in postman  

{         “时间戳记”:1547708533031,
        “状态”:500,
        “错误”:“内部服务器错误”,         “ exception”:“ org.springframework.http.converter.HttpMessageNotWritableException”,
        “ message”:“无法编写JSON:未为org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer类找到序列化器,也未发现创建BeanSerializer的属性(为避免异常,请禁用SerializationFeature.FAIL_ON_EMPTY_BEANS);嵌套异常为com.fasterxml .jackson.databind.JsonMappingException:未找到类org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer的序列化器,也未发现创建BeanSerializer的属性(为避免异常,请禁用SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过参考链:com.CetegoryEntites)。 .ArtistRegister [\“ subcetgory \”]-> com.Cetegory.Entites.SubCetegory _ $$ _ jvst706_1 [\“ handler \”])“”,
        “ path”:“ / getartist / 2”
    }

this is contoller for get data by id
@RestController
public class RegisterController {

    @Autowired
    ArtistService artser;

    @PostMapping(value="/addArtist",produces="application/json")
    @ResponseBody public ArtistRegister addArtist(@RequestBody ArtistRegister artist) {

        ArtistRegister artRegister = null;
        try {

            artRegister = artser.addArtist(artist);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return artRegister;

    }  

    @RequestMapping(value="/getartist/{artist_id}",method=RequestMethod.GET,produces="application/json")
    @ResponseBody   public ArtistRegister getArtistDetails(@PathVariable ("artist_id") int artist_id ,HttpServletRequest request,
        HttpServletResponse response) throws Exception{
    return artser.getArtistDetails(artist_id);

    }   

    @RequestMapping(value="/delete/{artist_id}",method=RequestMethod.DELETE,produces="application/json")
        public void deleteById(@PathVariable (value="artist_id") int artist_id,HttpServletRequest request,
                HttpServletResponse response) throws Exception{
            artser.deleteById(artist_id);
        }

    @RequestMapping(value = "/updateartist", method = RequestMethod.PUT, produces = "application/json")
    public ArtistRegister updateArtist(@RequestBody ArtistRegister artreg, HttpServletRequest request, HttpServletResponse response)
            throws Exception 
    {   
        return artser.updateArtist(artreg);
    }


this is service
@Service
@Transactional
public class ArtistService {

    @Autowired
    private ArtistRepository artrep;

    @Autowired
    private RegisterDAO artdao;

    public ArtistRegister addArtist(ArtistRegister artreg) {

        ArtistRegister artReg = null;
        try {
            artReg = artrep.save(artreg);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return artReg;
    }

    public ArtistRegister getArtistDetails(int artist_id) {
        return artdao.getArtistDetails(artist_id);

    }

    public void deleteById(int artist_id) {
        artdao.deleteById(artist_id);
    }

    public ArtistRegister updateArtist(ArtistRegister artreg) {
        return artdao.updateArtist(artreg);

    }

}


this is DAO
@Repository
@Transactional
public class RegisterDAO {

    private static final Logger logger = LoggerFactory.getLogger(SubCetegoryDAO.class);

    @Autowired
    SessionFactory sessionFactory;

    @Autowired
    EntityManager entitymanager;

    public ArtistRegister getArtistDetails(int artist_id) {

        try 
        {
            String hql = "FROM ArtistRegister a where a.artist_id=?";
            return (ArtistRegister) entitymanager.createQuery(hql).setParameter(1, artist_id).getSingleResult();
        } 
        catch (EmptyResultDataAccessException e) 
        {
            return null;
        } 
        catch (Exception e) 
        {
            logger.error("Exception in getUser"+ e.getMessage());
            return null;
        }

    }

1 个答案:

答案 0 :(得分:0)

通过以下方法删除@JsonManagedReference批注并将获取类型更新为LAZY:

 @OneToOne(targetEntity = SubCetegory.class, cascade = CascadeType.MERGE,fetch=FetchType.LAZY)
 @JoinColumn(name = "sub_cetegory_id")

什么是Lazy和Eager提取类型?

  

EAGER策略是持久性提供程序运行时的一项要求,必须急切地获取数据。 LAZY策略向持久性提供程序运行时发出提示,即在首次访问数据时应延迟获取数据。

参考

Official documentation fetch type