@@ -171,7 +171,7 @@ More samples for the Azure SQL output binding are available in the [GitHub repos
171171This section contains the following examples:
172172
173173* [ HTTP trigger, write a record to a table] ( #http-trigger-write-record-to-table-java )
174- <!-- * [HTTP trigger, write to two tables](#http-trigger-write-to-two-tables-java) -->
174+ * [ HTTP trigger, write to two tables] ( #http-trigger-write-to-two-tables-java )
175175
176176The examples refer to a ` ToDoItem ` class (in a separate file ` ToDoItem.java ` ) and a corresponding database table:
177177
@@ -233,6 +233,7 @@ public class PostToDo {
233233 public HttpResponseMessage run (
234234 @HttpTrigger (name = " req" , methods = {HttpMethod . POST }, authLevel = AuthorizationLevel . ANONYMOUS ) HttpRequestMessage<Optional<String > > request ,
235235 @SQLOutput (
236+ name = " sqlOutput" ,
236237 commandText = " dbo.ToDo" ,
237238 connectionStringSetting = " SqlConnectionString" )
238239 OutputBinding<ToDoItem > output ) throws JsonParseException , JsonMappingException , JsonProcessingException {
@@ -248,8 +249,6 @@ public class PostToDo {
248249}
249250```
250251
251- <!-- commented out until issue with java library resolved
252-
253252<a id =" http-trigger-write-to-two-tables-java " ></a >
254253### HTTP trigger, write to two tables
255254
@@ -267,9 +266,9 @@ The second table, `dbo.RequestLog`, corresponds to the following definition:
267266
268267``` sql
269268CREATE TABLE dbo .RequestLog (
270- Id int identity (1,1) primary key ,
271- RequestTimeStamp datetime2 not null ,
272- ItemCount int not null
269+ Id INT IDENTITY (1 ,1 ) PRIMARY KEY ,
270+ RequestTimeStamp DATETIME2 NOT NULL DEFAULT(GETDATE()) ,
271+ ItemCount INT NOT NULL
273272)
274273```
275274
@@ -298,32 +297,50 @@ public class RequestLog {
298297
299298
300299``` java
301- module.exports = async function (context, req) {
302- context.log('JavaScript HTTP trigger and SQL output binding function processed a request.');
303- context.log(req.body);
300+ package com.function ;
304301
305- const newLog = {
306- RequestTimeStamp = Date.now(),
307- ItemCount = 1
308- }
302+ import java.util.* ;
303+ import com.microsoft.azure.functions.annotation.* ;
304+ import com.microsoft.azure.functions.* ;
305+ import com.microsoft.azure.functions.sql.annotation.SQLOutput ;
306+ import com.fasterxml.jackson.core.JsonParseException ;
307+ import com.fasterxml.jackson.core.JsonProcessingException ;
308+ import com.fasterxml.jackson.databind.JsonMappingException ;
309+ import com.fasterxml.jackson.databind.ObjectMapper ;
309310
310- if (req.body) {
311- context.bindings.todoItems = req.body;
312- context.bindings.requestLog = newLog;
313- context.res = {
314- body: req.body,
315- mimetype: "application/json",
316- status: 201
317- }
318- } else {
319- context.res = {
320- status: 400,
321- body: "Error reading request body"
322- }
311+ import java.util.Optional ;
312+
313+ public class PostToDoWithLog {
314+ @FunctionName (" PostToDoWithLog" )
315+ public HttpResponseMessage run (
316+ @HttpTrigger (name = " req" , methods = {HttpMethod . POST }, authLevel = AuthorizationLevel . ANONYMOUS ) HttpRequestMessage<Optional<String > > request ,
317+ @SQLOutput (
318+ name = " sqlOutput" ,
319+ commandText = " dbo.ToDo" ,
320+ connectionStringSetting = " SqlConnectionString" )
321+ OutputBinding<ToDoItem > output ,
322+ @SQLOutput (
323+ name = " sqlOutput2" ,
324+ commandText = " dbo.RequestLog" ,
325+ connectionStringSetting = " SqlConnectionString" )
326+ OutputBinding<RequestLog > outputLog ,
327+ final ExecutionContext context ) throws JsonParseException , JsonMappingException , JsonProcessingException {
328+ context. getLogger(). info(" Java HTTP trigger processed a request." );
329+
330+ String json = request. getBody(). get();
331+ ObjectMapper mapper = new ObjectMapper ();
332+ ToDoItem newToDo = mapper. readValue(json, ToDoItem . class);
333+ newToDo. Id = UUID . randomUUID();
334+ output. setValue(newToDo);
335+
336+ RequestLog newLog = new RequestLog ();
337+ newLog. ItemCount = 1 ;
338+ outputLog. setValue(newLog);
339+
340+ return request. createResponseBuilder(HttpStatus . CREATED ). header(" Content-Type" , " application/json" ). body(output). build();
323341 }
324342}
325- ``` -->
326-
343+ ```
327344
328345::: zone-end
329346
0 commit comments