最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Spring Azure Function Adapter not honoring ResponseEntity - Stack Overflow

programmeradmin0浏览0评论

I'm trying to return a ResponseEntity for purposes of redirecting in response.

@PostMapping("/redirect")
ResponseEntity<String> checkRedirect() {
    String url = "/example";
    HttpHeaders headers = new HttpHeaders();
    headers.add("Location", "/example");
    return new ResponseEntity<>(url, headers, HttpStatus.FOUND);
}

This works fine with the Spring Web model. However, I'm using the Spring Cloud Function Azure Web Adapter to expose my REST mappings as Azure Functions. The response of my method is instead returned as an HttpStatus.OK (200), though the Location header is present. How can I get the correct return status to return?

I'm trying to return a ResponseEntity for purposes of redirecting in response.

@PostMapping("/redirect")
ResponseEntity<String> checkRedirect() {
    String url = "/example";
    HttpHeaders headers = new HttpHeaders();
    headers.add("Location", "/example");
    return new ResponseEntity<>(url, headers, HttpStatus.FOUND);
}

This works fine with the Spring Web model. However, I'm using the Spring Cloud Function Azure Web Adapter to expose my REST mappings as Azure Functions. The response of my method is instead returned as an HttpStatus.OK (200), though the Location header is present. How can I get the correct return status to return?

Share Improve this question edited Feb 3 at 18:25 end-user asked Jan 31 at 16:07 end-userend-user 2,9576 gold badges32 silver badges58 bronze badges 2
  • Provide your function code. – Pravallika KV Commented Feb 3 at 3:37
  • @PravallikaKV I think what you're asking for is the code that makes up the function. However, as I mention, I'm using Spring's "Azure Function Adapter" which generates the function code based off the Spring Web model. I can post the REST method that I'm using to demonstrate, but it's not actually the function code that's running. – end-user Commented Feb 3 at 18:24
Add a comment  | 

1 Answer 1

Reset to default 0

You can use below code to return the Location in response:

public class Function {
    @FunctionName("redirect")
    public HttpResponseMessage checkRedirect(
        @HttpTrigger(name = "req", methods = {HttpMethod.POST}, authLevel = AuthorizationLevel.FUNCTION) HttpRequestMessage<Optional<String>> request,
        final ExecutionContext context) {
        
        context.getLogger().info("Fetching the data");

        return request.createResponseBuilder(HttpStatus.FOUND)
                      .header("Location", "/example")
                      .build();
    }
}

I have created a Spring Boot Azure function where I could returns multiple values such as ID and Name in the response body.

Code Snippet:

public class HelloHandler extends FunctionInvoker<User, Greeting> {

    @FunctionName("hello")
    public HttpResponseMessage execute(
            @HttpTrigger(name = "request", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<User>> request,
            ExecutionContext context) {
        User user = request.getBody()
                .filter((u -> u.getName() != null))
                .orElseGet(() -> new User(1,
                        request.getQueryParameters()
                                .getOrDefault("name", "world")));
        context.getLogger().info("Greeting user name: " + user.getName());
        return request
                .createResponseBuilder(HttpStatus.OK)
                .body(handleRequest(user, context))
                .header("Content-Type", "application/json")
                .build();
    }
}

Hello.java:

@Component
public class Hello implements Function<Mono<User>, Mono<Greeting>> {

    public Mono<Greeting> apply(Mono<User> mono) {
        return mono.map(user -> new Greeting("Hello, " + user.getName()+" your id is:"+user.getId()));
    }
}

Response:

[2025-02-05T09:26:35.610Z] INFO: Starting application using Java 21.0.4 with PID 26620 (started by user in C:\XXX\my-spring-function)
[2025-02-05T09:26:35.613Z] Feb 05, 2025 2:56:35 PM .springframework.boot.SpringApplication logStartupProfileInfo
[2025-02-05T09:26:35.667Z] INFO: No active profile set, falling back to 1 default profile: "default"
[2025-02-05T09:26:37.020Z] Feb 05, 2025 2:56:37 PM .springframework.boot.StartupInfoLogger logStarted
[2025-02-05T09:26:37.023Z] INFO: Started application in 2.684 seconds (process running for 57.709)
[2025-02-05T09:26:37.040Z] Greeting user name: pravallika and id: 1
[2025-02-05T09:26:37.079Z] Function "hello" (Id: 899efba8-10f9-4772-8def-09e7af400334) invoked by Java Worker
[2025-02-05T09:26:37.146Z] Executed 'Functions.hello' (Succeeded, Id=899efba8-10f9-4772-8def-09e7af400334, Duration=3086ms) 

You can also use below code for PostMapping:

Refer my GitHub repository for code.

Code Snippet:

@Controller
public class CountryController {

    @Autowired
    CountryService countryService;

    @PostMapping("/countries")
    public String saveStudent(@ModelAttribute("country") Countries countries) {
        countryService.addCountry(country);
        return "redirect:/countries";
    }
}
发布评论

评论列表(0)

  1. 暂无评论