Doubt Springboot + Restful

Asked

Viewed 468 times

1

I’m having a problem with my Rest requests.

Until a while ago my code was working normally, but one to two weeks ago, my requests stopped working, except for GET and Delete methods

GET 
@RequestMapping(value = "/api/estabelecimentos", method = RequestMethod.GET, produces = "application/json")
        public @ResponseBody List<Estabelecimento> getAllEst() {
            return er.findAll();
        }

Delete
    @RequestMapping(method = RequestMethod.DELETE, value = "/api/estabelecimentos/{id}", produces = "application/json")
        public ResponseEntity<Estabelecimento> deleteEst(@PathVariable Long id) {
            er.deleteById(id);
            return new ResponseEntity<Estabelecimento>(HttpStatus.OK);
        }

The other methods (POST and PUT) return error 415 via client (Postman) and in the springboot log I get this return;

POST
@RequestMapping(method = RequestMethod.POST, value = "/api/estabelecimentos", produces = "application/json")
    public ResponseEntity<Estabelecimento> newEstabelecimento(@RequestBody Estabelecimento estabelecimento) {
        er.save(estabelecimento);
        return new ResponseEntity<Estabelecimento>(estabelecimento, HttpStatus.OK);
    }

PUT
@RequestMapping(method = RequestMethod.PUT, value = "/api/estabelecimentos/{id}", produces = "application/json")
    public ResponseEntity<Object> updadeEstabelecimento(@RequestBody Estabelecimento esta, @PathVariable Long id){
        Optional<Estabelecimento> estab = er.findById(id);

        if(!estab.isPresent()) 
            return ResponseEntity.notFound().build();

        esta.setCod_Estabelecimento(id);
        er.save(esta);
        return ResponseEntity.noContent().build();
    }

Log Springboot
14:24:36.716  WARN 14320 --- [nio-8080-exec-1] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class com.api.Entity.Estabelecimento]]: com.fasterxml.jackson.databind.JsonMappingException: Multiple back-reference properties with name 'defaultReference'
2019-05-22 14:24:36.720  WARN 14320 --- [nio-8080-exec-1] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class com.api.Entity.Estabelecimento]]: com.fasterxml.jackson.databind.JsonMappingException: Multiple back-reference properties with name 'defaultReference'
2019-05-22 14:24:36.724  WARN 14320 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported]

Retorno Clien(Postman)
"timestamp": "2019-05-22T17:24:36.740+0000",
    "status": 415,
    "error": "Unsupported Media Type",
    "message": "Content type 'application/json;charset=UTF-8' not supported",
    "trace": "org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported\r\n\tat org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:224)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:157)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:130)\r\n\tat org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgumen

Can anyone give me a light ? I couldn’t find anything on the internet

  • Send the Requisition Body and send the entity Establishment tbm

  • one to two weeks ago, my requests stop working - What change have you made in these two weeks to stop working?

  • Could post the Establishment entities and their sub-entities that are like attributes, because this error should be some cyclical reference, where the father calls the son, who then the son calls the parent or the child has more than one parent

2 answers

0

I decided as follows.

After analyzing Nullptr’s response, I was able to understand better what it was about

added a value for classes with relationship

Ex: Class A @Jsonmanagedreference(value = "identification name")

Class B @Jsonbackreference(value = "identification name")

  • do not forget to mark my answer as correct :)

  • haha' sorry! Marked!

0


The mistake that really matters in stacktrace is this:

com.fasterxml.jackson.databind.JsonMappingException: Multiple back-reference properties with name 'defaultReference'

You own or have added Annotation @JsonBackReference or @JsonManagedReference in your entity incorrectly.

Utilize @JsonManagedReference for collections and @JsonBackReference for simple relationships.


About the 415 - Unsupported Media Type, this was discussed in this and in this Spring thread, I agree it’s not the best error handling for a parse error (in my opinion it should be 422).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.