将 Content-Type 设置为 application/json 在 Java Azure Function 中

huangapple go评论113阅读模式
英文:

Setting the Content-Type to application/json in a Java Azure Function

问题

以下是您要翻译的内容:

I have developed a Java Azure function that is triggered by an HTTP Request, performs a query to CosmosDB, and return the retrieved data to the caller (front end). The problem is that the Content-Type of the Azure Function is plain text instead of application/json.

this is the code of my azure function:

  1. @FunctionName("backorders")
  2. public HttpResponseMessage run(
  3. @HttpTrigger(
  4. name = "req",
  5. methods = {HttpMethod.GET},
  6. authLevel = AuthorizationLevel.ANONYMOUS)
  7. HttpRequestMessage<Optional<String>> request,
  8. final ExecutionContext context) {
  9. context.getLogger().info("Java HTTP trigger processed a request.");
  10. try{
  11. createConnection();
  12. } catch (Exception e){
  13. System.out.println("An error occured when connecting to the DB");
  14. return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occured when connecting to the DB").build();
  15. }
  16. ArrayList<String> supplyPlannerIds = new ArrayList<>();
  17. try{
  18. Map<String,String> headers = request.getQueryParameters();
  19. System.out.println(headers.values());
  20. String param = headers.getOrDefault("spids", "");
  21. if(param.equals("")){
  22. System.out.println("The list of Supply Planner IDs can not be empty");
  23. return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("The list of Supply Planner IDs can not be empty").build();
  24. } else {
  25. System.out.println("Parsing the request body");
  26. supplyPlannerIds = new ArrayList<String>(Arrays.asList(param.split(",")));
  27. }
  28. } catch (Exception e){
  29. System.out.println("An error occured when fetching the SupplyPlannerIds");
  30. return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occured when retrieving the list of SupplyPlannerIds from the request").build();
  31. }
  32. BackorderListRepo orderRepo = new BackorderListRepo(container, supplyPlannerIds);
  33. ArrayList<Backorder> orders = orderRepo.retrieveBackorders();
  34. //String json = new Gson().toJson(orders);
  35. return request.createResponseBuilder(HttpStatus.OK).body(orders).build();
  36. }

this is my local.settings.json:

  1. {
  2. "IsEncrypted": false,
  3. "Values": {
  4. "AzureWebJobsStorage": "",
  5. "FUNCTIONS_WORKER_RUNTIME": "java"
  6. }
  7. }

this is my host.json

  1. {
  2. "version": "2.0",
  3. "extensionBundle": {
  4. "id": "Microsoft.Azure.Functions.ExtensionBundle",
  5. "version": "[3.*, 4.0.0)"
  6. }
  7. }

in particular when I call it using Postman this is what I get
postman call

I tried the following line of code but it didn't work
String json = new Gson().toJson(orders);

I also tried the following but it didn't work
request.getHeaders().put("content-type", "application/json");

thanks a lot in advance for your appreciated help 将 Content-Type 设置为 application/json 在 Java Azure Function 中

英文:

I have developed a Java Azure function that is triggered by an HTTP Request, performs a query to CosmosDB, and return the retrieved data to the caller (front end). The problem is that the Content-Type of the Azure Function is plain text instead of application/json.

this is the code of my azure function:

  1. @FunctionName(&quot;backorders&quot;)
  2. public HttpResponseMessage run(
  3. @HttpTrigger(
  4. name = &quot;req&quot;,
  5. methods = {HttpMethod.GET},
  6. authLevel = AuthorizationLevel.ANONYMOUS)
  7. HttpRequestMessage&lt;Optional&lt;String&gt;&gt; request,
  8. final ExecutionContext context) {
  9. context.getLogger().info(&quot;Java HTTP trigger processed a request.&quot;);
  10. try{
  11. createConnection();
  12. } catch (Exception e){
  13. System.out.println(&quot;An error occured when connecting to the DB&quot;);
  14. return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR).body(&quot;An error occured when connecting to the DB&quot;).build();
  15. }
  16. ArrayList&lt;String&gt; supplyPlannerIds = new ArrayList&lt;&gt;();
  17. try{
  18. Map&lt;String,String&gt; headers = request.getQueryParameters();
  19. System.out.println(headers.values());
  20. String param = headers.getOrDefault(&quot;spids&quot;, &quot;&quot;);
  21. if(param.equals(&quot;&quot;)){
  22. System.out.println(&quot;The list of Supply Planner IDs can not be empty&quot;);
  23. return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body(&quot;The list of Supply Planner IDs can not be empty&quot;).build();
  24. } else {
  25. System.out.println(&quot;Parsing the request body&quot;);
  26. supplyPlannerIds = new ArrayList&lt;String&gt;(Arrays.asList(param.split(&quot;,&quot;)));
  27. }
  28. } catch (Exception e){
  29. System.out.println(&quot;An error occured when fetching the SupplyPlannerIds&quot;);
  30. return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR).body(&quot;An error occured when retrieving the list of SupplyPlannerIds from the request&quot;).build();
  31. }
  32. BackorderListRepo orderRepo = new BackorderListRepo(container, supplyPlannerIds);
  33. ArrayList&lt;Backorder&gt; orders = orderRepo.retrieveBackorders();
  34. //String json = new Gson().toJson(orders);
  35. return request.createResponseBuilder(HttpStatus.OK).body(orders).build();
  36. }

this is my local.settings.json:

  1. {
  2. &quot;IsEncrypted&quot;: false,
  3. &quot;Values&quot;: {
  4. &quot;AzureWebJobsStorage&quot;: &quot;&quot;,
  5. &quot;FUNCTIONS_WORKER_RUNTIME&quot;: &quot;java&quot;
  6. }
  7. }

this is my host.json

  1. {
  2. &quot;version&quot;: &quot;2.0&quot;,
  3. &quot;extensionBundle&quot;: {
  4. &quot;id&quot;: &quot;Microsoft.Azure.Functions.ExtensionBundle&quot;,
  5. &quot;version&quot;: &quot;[3.*, 4.0.0)&quot;
  6. }
  7. }

in particular when I call it using Postman this is what I get
postman call

I tried the following line of code but it didn't work
String json = new Gson().toJson(orders);

I also tried the following but it didn't work
request.getHeaders().put(&quot;content-type&quot;, &quot;application/json&quot;);

thanks a lot in advance for your appreciated help 将 Content-Type 设置为 application/json 在 Java Azure Function 中

答案1

得分: 0

你差不多到了那个地方:在HTTP中,有分开的请求和响应头,你尝试添加一个请求头而不是响应头。

在你的最后一句中尝试这个:

  1. return request.createResponseBuilder(HttpStatus.OK).header(&quot;Content-Type&quot;, &quot;application/json&quot;).body(orders).build();
英文:

You were nearly there: in HTTP there are separated headers for request and response, and you tried to add a request header instead of a response header.

Try this in your last statement:

  1. return request.createResponseBuilder(HttpStatus.OK).header(&quot;Content-Type&quot;, &quot;application/json&quot;).body(orders).build();

huangapple
  • 本文由 发表于 2023年6月14日 23:22:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475157.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定