Skip to content

Commit c87b201

Browse files
Merge pull request #261588 from lucyzhang929/luczhan/sqloutputjavasampleupdate
Update sql output java samples
2 parents 07c6f25 + 0d0c8b0 commit c87b201

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

articles/azure-functions/functions-bindings-azure-sql-output.md

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,13 @@ public class ToDoItem {
376376
<a id="http-trigger-write-record-to-table-java"></a>
377377
### HTTP trigger, write a record to a table
378378

379-
The following example shows a SQL output binding in a Java function that adds a record to a table, using data provided in an HTTP POST request as a JSON body. The function takes an additional dependency on the [com.fasterxml.jackson.core](https://github.com/FasterXML/jackson) library to parse the JSON body.
379+
The following example shows a SQL output binding in a Java function that adds a record to a table, using data provided in an HTTP POST request as a JSON body. The function takes an additional dependency on the [com.google.code.gson](https://github.com/google/gson) library to parse the JSON body.
380380

381381
```xml
382382
<dependency>
383-
<groupId>com.fasterxml.jackson.core</groupId>
384-
<artifactId>jackson-databind</artifactId>
385-
<version>2.13.4.1</version>
383+
<groupId>com.google.code.gson</groupId>
384+
<artifactId>gson</artifactId>
385+
<version>2.10.1</version>
386386
</dependency>
387387
```
388388

@@ -393,10 +393,7 @@ import java.util.*;
393393
import com.microsoft.azure.functions.annotation.*;
394394
import com.microsoft.azure.functions.*;
395395
import com.microsoft.azure.functions.sql.annotation.SQLOutput;
396-
import com.fasterxml.jackson.core.JsonParseException;
397-
import com.fasterxml.jackson.core.JsonProcessingException;
398-
import com.fasterxml.jackson.databind.JsonMappingException;
399-
import com.fasterxml.jackson.databind.ObjectMapper;
396+
import com.google.gson.Gson;
400397

401398
import java.util.Optional;
402399

@@ -408,10 +405,10 @@ public class PostToDo {
408405
name = "toDoItem",
409406
commandText = "dbo.ToDo",
410407
connectionStringSetting = "SqlConnectionString")
411-
OutputBinding<ToDoItem> output) throws JsonParseException, JsonMappingException, JsonProcessingException {
408+
OutputBinding<ToDoItem> output) {
412409
String json = request.getBody().get();
413-
ObjectMapper mapper = new ObjectMapper();
414-
ToDoItem newToDo = mapper.readValue(json, ToDoItem.class);
410+
Gson gson = new Gson();
411+
ToDoItem newToDo = gson.fromJson(json, ToDoItem.class);
415412

416413
newToDo.Id = UUID.randomUUID();
417414
output.setValue(newToDo);
@@ -424,13 +421,13 @@ public class PostToDo {
424421
<a id="http-trigger-write-to-two-tables-java"></a>
425422
### HTTP trigger, write to two tables
426423

427-
The following example shows a SQL output binding in a JavaS function that adds records to a database in two different tables (`dbo.ToDo` and `dbo.RequestLog`), using data provided in an HTTP POST request as a JSON body and multiple output bindings. The function takes an additional dependency on the [com.fasterxml.jackson.core](https://github.com/FasterXML/jackson) library to parse the JSON body.
424+
The following example shows a SQL output binding in a JavaS function that adds records to a database in two different tables (`dbo.ToDo` and `dbo.RequestLog`), using data provided in an HTTP POST request as a JSON body and multiple output bindings. The function takes an additional dependency on the [com.google.code.gson](https://github.com/google/gson) library to parse the JSON body.
428425

429426
```xml
430427
<dependency>
431-
<groupId>com.fasterxml.jackson.core</groupId>
432-
<artifactId>jackson-databind</artifactId>
433-
<version>2.13.4.1</version>
428+
<groupId>com.google.code.gson</groupId>
429+
<artifactId>gson</artifactId>
430+
<version>2.10.1</version>
434431
</dependency>
435432
```
436433

@@ -475,10 +472,7 @@ import java.util.*;
475472
import com.microsoft.azure.functions.annotation.*;
476473
import com.microsoft.azure.functions.*;
477474
import com.microsoft.azure.functions.sql.annotation.SQLOutput;
478-
import com.fasterxml.jackson.core.JsonParseException;
479-
import com.fasterxml.jackson.core.JsonProcessingException;
480-
import com.fasterxml.jackson.databind.JsonMappingException;
481-
import com.fasterxml.jackson.databind.ObjectMapper;
475+
import com.google.gson.Gson;
482476

483477
import java.util.Optional;
484478

@@ -496,12 +490,12 @@ public class PostToDoWithLog {
496490
commandText = "dbo.RequestLog",
497491
connectionStringSetting = "SqlConnectionString")
498492
OutputBinding<RequestLog> outputLog,
499-
final ExecutionContext context) throws JsonParseException, JsonMappingException, JsonProcessingException {
493+
final ExecutionContext context) {
500494
context.getLogger().info("Java HTTP trigger processed a request.");
501495

502496
String json = request.getBody().get();
503-
ObjectMapper mapper = new ObjectMapper();
504-
ToDoItem newToDo = mapper.readValue(json, ToDoItem.class);
497+
Gson gson = new Gson();
498+
ToDoItem newToDo = gson.fromJson(json, ToDoItem.class);
505499
newToDo.Id = UUID.randomUUID();
506500
output.setValue(newToDo);
507501

0 commit comments

Comments
 (0)