英文:
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:
@FunctionName("backorders")
public HttpResponseMessage run(
@HttpTrigger(
name = "req",
methods = {HttpMethod.GET},
authLevel = AuthorizationLevel.ANONYMOUS)
HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
context.getLogger().info("Java HTTP trigger processed a request.");
try{
createConnection();
} catch (Exception e){
System.out.println("An error occured when connecting to the DB");
return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occured when connecting to the DB").build();
}
ArrayList<String> supplyPlannerIds = new ArrayList<>();
try{
Map<String,String> headers = request.getQueryParameters();
System.out.println(headers.values());
String param = headers.getOrDefault("spids", "");
if(param.equals("")){
System.out.println("The list of Supply Planner IDs can not be empty");
return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("The list of Supply Planner IDs can not be empty").build();
} else {
System.out.println("Parsing the request body");
supplyPlannerIds = new ArrayList<String>(Arrays.asList(param.split(",")));
}
} catch (Exception e){
System.out.println("An error occured when fetching the SupplyPlannerIds");
return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occured when retrieving the list of SupplyPlannerIds from the request").build();
}
BackorderListRepo orderRepo = new BackorderListRepo(container, supplyPlannerIds);
ArrayList<Backorder> orders = orderRepo.retrieveBackorders();
//String json = new Gson().toJson(orders);
return request.createResponseBuilder(HttpStatus.OK).body(orders).build();
}
this is my local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "java"
}
}
this is my host.json
{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[3.*, 4.0.0)"
}
}
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
英文:
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:
@FunctionName("backorders")
public HttpResponseMessage run(
@HttpTrigger(
name = "req",
methods = {HttpMethod.GET},
authLevel = AuthorizationLevel.ANONYMOUS)
HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
context.getLogger().info("Java HTTP trigger processed a request.");
try{
createConnection();
} catch (Exception e){
System.out.println("An error occured when connecting to the DB");
return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occured when connecting to the DB").build();
}
ArrayList<String> supplyPlannerIds = new ArrayList<>();
try{
Map<String,String> headers = request.getQueryParameters();
System.out.println(headers.values());
String param = headers.getOrDefault("spids", "");
if(param.equals("")){
System.out.println("The list of Supply Planner IDs can not be empty");
return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("The list of Supply Planner IDs can not be empty").build();
} else {
System.out.println("Parsing the request body");
supplyPlannerIds = new ArrayList<String>(Arrays.asList(param.split(",")));
}
} catch (Exception e){
System.out.println("An error occured when fetching the SupplyPlannerIds");
return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occured when retrieving the list of SupplyPlannerIds from the request").build();
}
BackorderListRepo orderRepo = new BackorderListRepo(container, supplyPlannerIds);
ArrayList<Backorder> orders = orderRepo.retrieveBackorders();
//String json = new Gson().toJson(orders);
return request.createResponseBuilder(HttpStatus.OK).body(orders).build();
}
this is my local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "java"
}
}
this is my host.json
{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[3.*, 4.0.0)"
}
}
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
答案1
得分: 0
你差不多到了那个地方:在HTTP中,有分开的请求和响应头,你尝试添加一个请求头而不是响应头。
在你的最后一句中尝试这个:
return request.createResponseBuilder(HttpStatus.OK).header("Content-Type", "application/json").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:
return request.createResponseBuilder(HttpStatus.OK).header("Content-Type", "application/json").body(orders).build();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论