मैं एक साधारण सीआरयूडी एपीआई विकसित कर रहा हूं लेकिन मेरी पोस्ट विधि काम नहीं कर रही है, हर बार जेसन प्रारूप के साथ डेटा भेजने पर यह अपवाद फेंकता है

{
    "title": "Cannot construct instance of `academy.devdojo.springboot2.requests.AnimePostRequestBody` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)\n at [Source: (PushbackInputStream); line: 2, column: 5]",
    "status": 400,
    "details": "JSON parse error: Cannot construct instance of `academy.devdojo.springboot2.requests.AnimePostRequestBody` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `academy.devdojo.springboot2.requests.AnimePostRequestBody` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)\n at [Source: (PushbackInputStream); line: 2, column: 5]",
    "developerMessage": "org.springframework.http.converter.HttpMessageNotReadableException",
    "timestamp": "2022-01-13T14:45:34.9138659"
}

यह केवल तभी काम करता है जब कोई साधारण स्ट्रिंग भेजता है जैसे "कुछ नाम सहेजा जाना है", नहीं

{
  "name" : "some name to be saved"
}

यहाँ मेरी कक्षाएं हैं

कंपनी

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Builder
public class Anime {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotEmpty(message= "anime name cannot be empty")
    private String name;

}

रेपो

public interface AnimeRepository extends JpaRepository<Anime, Long> {
    List<Anime> findByName(String name);
}

सेवा

@Service
@RequiredArgsConstructor
public class AnimeService {

    private final AnimeRepository animeRepository;

    public Page<Anime> listAll(Pageable pageable) {

        return animeRepository.findAll(pageable);
    }

    public List<Anime> findByName(String name) {
        List<Anime> byName = animeRepository.findByName(name);
        if (byName.isEmpty()){
            throw new BadRequestException("Anime not Found");
        }
        return byName;
    }

    public Anime findByIdOrThrowBadRequestException(long id) {
        return animeRepository.findById(id)
                .orElseThrow(() -> new BadRequestException("Anime not Found"));
    }


    public Anime save(AnimePostRequestBody animePostRequestBody) {

        Anime anime = AnimeMapper.INSTANCE.toAnime(animePostRequestBody);

        return animeRepository.save(anime);
    }

    public void delete(long id) {

        animeRepository.delete(findByIdOrThrowBadRequestException(id));
    }

    public void replace(AnimePutRequestBody animePutRequestBody) {

        Anime savedAnime  = findByIdOrThrowBadRequestException(animePutRequestBody.getId());

        Anime anime = AnimeMapper.INSTANCE.toAnime(animePutRequestBody);

        anime.setId(savedAnime.getId());

        animeRepository.save(anime);
    }

    public List<Anime> listAllNonPageable() {
        return animeRepository.findAll();
    }
}

आराम नियंत्रक

@RestController
@Api(tags = "Atividades Economicas")
@RequestMapping("animes")
@Log4j2
@RequiredArgsConstructor
public class AnimeController {
    private final DateUtil dateUtil;
    private final AnimeService animeService;

    @ApiOperation(value = "Listar todos")
    @GetMapping
    public ResponseEntity<Page<Anime>> list(Pageable pageable) {
        //log.info(dateUtil.formatLocalDateTimeToDatabaseStyle(LocalDateTime.now()));
        return ResponseEntity.ok(animeService.listAll(pageable));
    }

    @GetMapping(path = "/all")
    public ResponseEntity<List<Anime>> listAll() {
        //log.info(dateUtil.formatLocalDateTimeToDatabaseStyle(LocalDateTime.now()));
        return ResponseEntity.ok(animeService.listAllNonPageable());
    }
    @ApiOperation(value = "Listar por ID")
    @GetMapping(path = "/{id}")
    public ResponseEntity<Anime> findById(@PathVariable long id) {
        return ResponseEntity.ok(animeService.findByIdOrThrowBadRequestException(id));
    }

    @ApiOperation(value = "Encontrar pelo nome")
    @GetMapping(path = "/find")
    public ResponseEntity<List<Anime>> findByName(@RequestParam String name) {
        return ResponseEntity.ok(animeService.findByName(name));
    }


    @PostMapping
    public ResponseEntity<Anime> save(@RequestBody @Valid AnimePostRequestBody animePostRequestBody) {
        return new ResponseEntity<>(animeService.save(animePostRequestBody), HttpStatus.CREATED);
    }


    @DeleteMapping(path = "/{id}")
    public ResponseEntity<Void> delete(@PathVariable long id) {
        animeService.delete(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PutMapping
    public ResponseEntity<Void> replace(@RequestBody AnimePutRequestBody animePutRequestBody) {
        animeService.replace(animePutRequestBody);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}

डीटीओ के

@Data
@Builder
public class AnimePostRequestBody {

   @NotEmpty(message= "anime name cannot be empty")
    private String name;

}

@Data
@Builder
public class AnimePutRequestBody {
    private Long id;
    private String name;
}

मैपर

@Mapper(componentModel = "spring")
public abstract class AnimeMapper {

    public static  final AnimeMapper INSTANCE = Mappers.getMapper(AnimeMapper.class);

    public abstract Anime toAnime(AnimePostRequestBody animePostRequestBody);
    public abstract Anime toAnime(AnimePutRequestBody animePutRequestBody);
}

अनुलेख पुट विधि ठीक काम कर रही है, हालांकि वे बहुत समान हैं।

0
amanda nascimento 13 जिंदा 2022, 21:03

4 जवाब

@Data एक सुविधाजनक शॉर्टकट एनोटेशन है जो @ToString, @EqualsAndHashCode, @Getter / @Setter और @RequiredArgsConstructor की सुविधाओं को एक साथ जोड़ता है

डिफ़ॉल्ट कंस्ट्रक्टर की आवश्यकता है। लोम्बोक का @NoArgsConstructor एनोटेशन जोड़ें।

1
Udit kaushik 14 जिंदा 2022, 13:18

मॉडल वर्ग के साथ-साथ, DTO वर्ग में @AllArgsConstructor और @NoArgsConstructor भी होने चाहिए। डीटीओ के लिए RequestBody मानों को मैप करने के लिए इसे कंस्ट्रक्टर्स की आवश्यकता होगी और @Data @AllArgsConstructor और @NoArgsConstructor कंस्ट्रक्टर प्रदान नहीं करता है।

0
Vikas Vaibhav 14 जिंदा 2022, 05:12

//कंपनी

    @Data
    @Entity
    @Table(name = "Anime")
    @Builder
    public class Anime {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotEmpty(message= "anime name cannot be empty")
    private String name;

}

// रेपो

@Repository
public interface AnimeRepository extends JpaRepository<Anime, Long> {
}

// डीटीओ

@Getter
public class AnimePostRequestBody {

    @NotEmpty(message = "anime name cannot by empty")
    private String name;
}

// नियंत्रक

@RestController
@RequestMapping("animes")
public class AnimeController {

    @Autowired
    private AnimeService animeService;

    @PostMapping
    public ResponseEntity<Anime> save(@RequestBody @Valid AnimePostRequestBody animePostRequestBody) {
        return new ResponseEntity<>(animeService.save(animePostRequestBody), HttpStatus.CREATED);
    }


}

//सेवा

@Service
public class AnimeService {

    @Autowired
    private AnimeRepository animeRepository;

    public Anime save(AnimePostRequestBody animePostRequestBody) {
        Anime anime = Anime.builder()
                .name(animePostRequestBody.getName())
                .build();
        return animeRepository.save(anime);
    }
}

आउटपुट

<मजबूत>{ "आईडी": 4, "नाम": "नारुतो" }

0
Maheen N 14 जिंदा 2022, 09:05

इस लाइन को बदलें

 public ResponseEntity<Anime> save(@RequestBody @Valid AnimePostRequestBody animePostRequestBody){..

इसके साथ ही:

 public ResponseEntity<Anime> save(@Valid @RequestBody AnimePostRequestBody animePostRequestBody)
0
user404 14 जिंदा 2022, 12:48