0

When I add "Accept": "application/xml" in the HTTP headers, my application returns empty content and HttpMediaTypeNotAcceptableException in the console. I don't know how to handle it to get a response like this:

{
    "status": 406,
    "message": "{message}"
}

Here is the controller class I try to catch this error in, but this does not work. I was also trying to use @ExceptionHandler annotation but it also doesn't seem to work.

package com.example.zadanie.controller;

import com.example.zadanie.model.ErrorModel;
import com.example.zadanie.model.RepoModel;
import com.example.zadanie.service.RepoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpMediaTypeException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.HttpClientErrorException;

import java.util.List;

@org.springframework.stereotype.Controller
public class Controller {
    private RepoService repoService;

    @Autowired
    public Controller(RepoService repoService) {
        this.repoService = repoService;
    }

    @GetMapping("/{username}")
    public ResponseEntity<List<RepoModel>> getRepoList(@PathVariable String username) {
        try {
            return new ResponseEntity(repoService.repoModels(username), HttpStatus.OK);
        }
        catch (HttpClientErrorException e) {
            ErrorModel errorModel = new ErrorModel(e.getStatusCode().value(), "user does not exist");
            return new ResponseEntity(errorModel, HttpStatus.NOT_FOUND);
        }
    }

//    @GetMapping
//    @ExceptionHandler(HttpMediaTypeNotAcceptableException.class)
//    public ResponseEntity httpMediaTypeNotAcceptableExceptionHandler(){
//        return new ResponseEntity(new ErrorModel(HttpStatus.NOT_ACCEPTABLE.value(), "this application accept only json response"),HttpStatus.NOT_ACCEPTABLE);
//    }
}

Service class

Here I download repos from github with its APIs.

package com.example.zadanie.service;

import com.example.zadanie.model.RepoModel;
import com.example.zadanie.model.ResponseModel;
import org.springframework.http.HttpStatusCode;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@Service
public class RepoService {

    public List<ResponseModel> repoModels(String username){
        List<ResponseModel> responseModelList = new ArrayList<>();
        RestTemplate restTemplate = new RestTemplate();

            RepoModel[] repoModels = restTemplate.getForObject("https://api.github.com/users/" + username + "/repos", RepoModel[].class);
            for (RepoModel repoModel : repoModels) {
                ResponseModel responseModel = new ResponseModel();
                responseModel.setName(repoModel.getName());
                responseModel.setLogin(repoModel.getOwner().getLogin());
                responseModelList.add(responseModel);
            }
            return responseModelList;
    }


}

ErrorModel class

package com.example.zadanie.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;

public class ErrorModel {
    @JsonProperty("status")
    private int httpStatusCode;
    @JsonProperty("message")
    private String message;

    public ErrorModel() {
    }

    public ErrorModel(int httpStatusCode, String message) {
        this.httpStatusCode = httpStatusCode;
        this.message = message;
    }

    public int getHttpStatusCode() {
        return httpStatusCode;
    }

    public void setHttpStatusCode(int httpStatusCode) {
        this.httpStatusCode = httpStatusCode;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return "ErrorModel{" +
                "httpStatusCode=" + httpStatusCode +
                ", message='" + message + '\'' +
                '}';
    }
}

How can I handle this exception the right way, and get an output formatted like shown earlier?

0 Answers0