@@ -171,7 +171,7 @@ More samples for the Azure SQL output binding are available in the [GitHub repos
171
171
This section contains the following examples:
172
172
173
173
* [ 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 )
175
175
176
176
The examples refer to a ` ToDoItem ` class (in a separate file ` ToDoItem.java ` ) and a corresponding database table:
177
177
@@ -233,6 +233,7 @@ public class PostToDo {
233
233
public HttpResponseMessage run (
234
234
@HttpTrigger (name = " req" , methods = {HttpMethod . POST }, authLevel = AuthorizationLevel . ANONYMOUS ) HttpRequestMessage<Optional<String > > request ,
235
235
@SQLOutput (
236
+ name = " sqlOutput" ,
236
237
commandText = " dbo.ToDo" ,
237
238
connectionStringSetting = " SqlConnectionString" )
238
239
OutputBinding<ToDoItem > output ) throws JsonParseException , JsonMappingException , JsonProcessingException {
@@ -248,8 +249,6 @@ public class PostToDo {
248
249
}
249
250
```
250
251
251
- <!-- commented out until issue with java library resolved
252
-
253
252
<a id =" http-trigger-write-to-two-tables-java " ></a >
254
253
### HTTP trigger, write to two tables
255
254
@@ -267,9 +266,9 @@ The second table, `dbo.RequestLog`, corresponds to the following definition:
267
266
268
267
``` sql
269
268
CREATE 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
273
272
)
274
273
```
275
274
@@ -298,32 +297,50 @@ public class RequestLog {
298
297
299
298
300
299
``` 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 ;
304
301
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 ;
309
310
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();
323
341
}
324
342
}
325
- ``` -->
326
-
343
+ ```
327
344
328
345
::: zone-end
329
346
0 commit comments