admin管理员组文章数量:1312741
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?
- 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
1 Answer
Reset to default 0You 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";
}
}
本文标签: Spring Azure Function Adapter not honoring ResponseEntityStack Overflow
版权声明:本文标题:Spring Azure Function Adapter not honoring ResponseEntity - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741907685a2404246.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论