Backend Development 6 min read

How to Use OpenFeign 4.1.2: Parameter Constraints & Custom Status Codes

This article explains the two new features introduced in Spring Cloud OpenFeign 4.1.2—request parameter constraints and custom status codes—by providing step‑by‑step examples, code snippets, and configuration details to demonstrate how to enable and use these capabilities in a SpringBoot 3.3.1 microservice environment.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
How to Use OpenFeign 4.1.2: Parameter Constraints & Custom Status Codes

Environment

SpringBoot 3.3.1 + Spring Cloud 2023.0.2

1. Introduction

OpenFeign is a declarative HTTP client in Spring Cloud that simplifies remote service calls. The latest version 4.1.2 adds two previously unsupported features: request parameter condition limiting and custom status codes.

2. Practical Examples

2.1 Request Parameter Constraints

Remote interface

<code>@GetMapping(value = "/params/user", params = "action=query")
public String queryUser() {
    return "query user";
}
</code>

The params attribute forces the request to contain action=query ; otherwise an error occurs.

Feign client declaration

<code>@FeignClient(name = "param", url = "http://localhost:8080")
public interface ParamFeign {
    @GetMapping(value = "/params/user", params = "action=query")
    String queryUser();
}
</code>

Note: the Feign method must match the remote interface exactly.

Calling this Feign client with OpenFeign 4.1.1 results in the error "request does not contain action=query" . Upgrading Spring Cloud to version 2023.0.2 (which uses OpenFeign 4.1.2) resolves the issue.

<code>&lt;spring-cloud.version&gt;2023.0.2&lt;/spring-cloud.version&gt;
</code>

2.2 Custom Status Codes

Remote interface

<code>@RestController
public class ParamController {
    @GetMapping("/api/status")
    public ResponseEntity<String> status() {
        return ResponseEntity.status(289).body("Custom 289 - status code");
    }
}
</code>

Status 289 is not defined in the HTTP standard (only 2xx codes are defined). The service responds correctly, but OpenFeign 4.1.1 treats it as an error.

Feign client declaration (still 4.1.1)

<code>@FeignClient(name = "param", url = "http://localhost:8080")
public interface ParamFeign {
    @GetMapping("/api/status")
    ResponseEntity<String> status();
}
</code>

Calling the Feign method returns an error because the status code is not recognized.

After upgrading to OpenFeign 4.1.2, custom status codes are supported, but only within the 200‑300 range. Example with status 333:

<code>@GetMapping("/api/status")
public ResponseEntity<String> status() {
    return ResponseEntity.status(333).body("Custom 333 - status code");
}
</code>

Calling this endpoint works via Postman, but Feign throws an exception because the internal rule evaluates status > 300 as false.

Conclusion

OpenFeign 4.1.2 introduces request parameter constraints and support for custom status codes (limited to 200‑300). Upgrading Spring Cloud to version 2023.0.2 is required to use these features.

JavamicroservicesBackend DevelopmentSpring CloudOpenFeign
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.