Validate accept Headers in spring

binaryhex :

How can I validate the accept headers in spring and return a custom message if the accept header is not application/json. Currently I am doing it this way(below) but I am wondering if there is another way of doing this?

I know I can make custom exceptions and throw the exception based on what went wrong but is there a different way of doing this?

@GetMapping(value = "/get", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public ResponseEntity<String> get(final HttpEntity<String> httpEntity) {
    HttpHeaders responseHeaders = new HttpHeaders();
    String acceptString = httpEntity.getHeaders().getFirst("Accept");

    if (acceptString == null || acceptString.isEmpty()) {
        return new ResponseEntity<String>("empty accept header", HttpStatus.BAD_REQUEST);
    }

    if ((MediaType.APPLICATION_JSON_VALUE).equalsIgnoreCase(acceptString)) {
        responseHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
    }
    else {
        return new ResponseEntity<String>("Not valid accept value", HttpStatus.BAD_REQUEST);
    }

....
Bart :

Within your @Getmapping you can add headers="Accept=application/json" this way your method will only handle calls with that header type.

You can then add a method which processes all the calls that are ignored by your specific AcceptHeader method. This way you only need one method to validate the headers.

You can also write a custom validator and use anotation to add this to your method like here: conditional validation

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Ignore Accept header in spring mvc

Spring Boot Rest - How to accept multiple headers

Validate Bean in Spring boot

How to validate CSV headers using Univocity routines?

What is q=0.5 in Accept* HTTP headers?

Accept arbitrary JSON as RequestBody in Spring

Spring @RequestMapping headers OR instead of AND?

Spring - Validate the field of a field

406 Spring MVC Json, not acceptable according to the request "accept" headers

How to interpret multiple Accept-* headers

Have Apache Accept LF vs CRLF in Request Headers

Spring Boot CORS headers

VBA Word - Conditionally Accept Changes in Headers and Footers

How to specify Accept headers from rest_framework.test.Client?

Boost beast manupulate http response headers durring accept

Kentico - failure to validate security headers

Can ActiveMQ accept messages with Enum headers from Camel?

jQuery Validate adding accept argument breaks code

Spring 4 RestController JSON: characteristics not acceptable according to the request "accept" headers

Validate part of object Spring

Spring Error: only capable of generating responses with characteristics not acceptable according to the request "accept" headers

http accept and content-type headers confusion

How to validate react prop that should accept Component

Easy way to copy Accept Language headers to outgoing request?

Mask Headers in spring restdocs

Do HTTP DELETEs require Content-Type & Accept Headers

How to validate headers and body in Gin-Gonic?

JavaScript - Validate Integer and accept dot, comma

Spring Boot Mapper no accept Autowired