CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Reposted on 2025-02-05
by Pravallika KV

Original Post

Original - Posted on 2024-12-19
by Pravallika KV



            
Present in both answers; Present only in the new answer; Present only in the old answer;

**You can use below code to return the Location in response:** ```java public class Function { @FunctionName("redirect") public HttpResponseMessage checkRedirect( @HttpTrigger(name = "req", methods = {HttpMethod.POST}, authLevel = AuthorizationLevel.FUNCTION) HttpRequestMessage<Optional<String>> request, final ExecutionContext context) { context.getLogger().info("Fetching the data");
return request.createResponseBuilder(HttpStatus.FOUND) .header("Location", "/example") .build(); } } ```
I have created a Spring Boot Azure function where I could returns multiple values such as **ID** and **Name** in the response body.
**Code Snippet:** ```java public class HelloHandler extends FunctionInvoker<User, Greeting> {
@FunctionName("hello") public HttpResponseMessage execute( @HttpTrigger(name = "request", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<User>> request, ExecutionContext context) { User user = request.getBody() .filter((u -> u.getName() != null)) .orElseGet(() -> new User(1, request.getQueryParameters() .getOrDefault("name", "world"))); context.getLogger().info("Greeting user name: " + user.getName()); return request .createResponseBuilder(HttpStatus.OK) .body(handleRequest(user, context)) .header("Content-Type", "application/json") .build(); } } ``` **Hello.java:** ```java @Component public class Hello implements Function<Mono<User>, Mono<Greeting>> {
public Mono<Greeting> apply(Mono<User> mono) { return mono.map(user -> new Greeting("Hello, " + user.getName()+" your id is:"+user.getId())); } } ``` **Response:** ```java [2025-02-05T09:26:35.610Z] INFO: Starting application using Java 21.0.4 with PID 26620 (started by user in C:\XXX\my-spring-function) [2025-02-05T09:26:35.613Z] Feb 05, 2025 2:56:35 PM org.springframework.boot.SpringApplication logStartupProfileInfo [2025-02-05T09:26:35.667Z] INFO: No active profile set, falling back to 1 default profile: "default" [2025-02-05T09:26:37.020Z] Feb 05, 2025 2:56:37 PM org.springframework.boot.StartupInfoLogger logStarted [2025-02-05T09:26:37.023Z] INFO: Started application in 2.684 seconds (process running for 57.709) [2025-02-05T09:26:37.040Z] Greeting user name: pravallika and id: 1 [2025-02-05T09:26:37.079Z] Function "hello" (Id: 899efba8-10f9-4772-8def-09e7af400334) invoked by Java Worker [2025-02-05T09:26:37.146Z] Executed 'Functions.hello' (Succeeded, Id=899efba8-10f9-4772-8def-09e7af400334, Duration=3086ms) ``` ![enter image description here](https://i.imgur.com/ITgorut.png)
**You can also use below code for PostMapping:**
Refer my [GitHub repository](https://github.com/PravallikaKothaveerannagari/Springboot_H2-database_CRUD) for code.
**Code Snippet:** ```java @Controller public class CountryController {
@Autowired CountryService countryService;
@PostMapping("/countries") public String saveStudent(@ModelAttribute("country") Countries countries) { countryService.addCountry(country); return "redirect:/countries"; } } ```
- **Point the `Start-Class` in `pom.xml` to the main class of your spring boot application.** ``` <start-class>com.example.DemoApplication</start-class> ``` - or Add the Application setting `Main_Class = com.example.DemoApplication` in the `FunctionApp=>Settings=>Environment Variables=>App Settings`.
I have created a simple Spring Boot Azure function, refer [GitHub repository](https://github.com/Azure-Samples/hello-spring-function-azure) for the sample project.
Run the project locally before deploying to Azure and check if you are able to sync the function trigger.
**Update `pom.xml` as below:**
**pom.xml:**
```xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId> <artifactId>hello</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging>
<name>Hello Spring Function on Azure</name>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.1</version> <relativePath/> </parent>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target>
<azure.functions.maven.plugin.version>1.22.0</azure.functions.maven.plugin.version> <azure.functions.java.library.version>3.0.0</azure.functions.java.library.version> <spring.cloud.function.dependencies>4.0.0</spring.cloud.function.dependencies> <functionResourceGroup>resource_group_name</functionResourceGroup> <functionAppServicePlanName>AppService_Plan</functionAppServicePlanName> <functionAppName>Function_App_Name</functionAppName> <functionAppRegion>Region</functionAppRegion> <functionPricingTier>Pricing Tier</functionPricingTier>
<start-class>com.example.DemoApplication</start-class> </properties>
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-function-adapter-azure</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-function-webflux</artifactId> <scope>provided</scope> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.gatling.highcharts</groupId> <artifactId>gatling-charts-highcharts</artifactId> <version>3.9.0</version> <scope>test</scope> </dependency> </dependencies>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-function-dependencies</artifactId> <version>${spring.cloud.function.dependencies}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.microsoft.azure.functions</groupId> <artifactId>azure-functions-java-library</artifactId> <version>${azure.functions.java.library.version}</version> </dependency> </dependencies> </dependencyManagement>
<build> <pluginManagement> <plugins> <plugin> <groupId>com.microsoft.azure</groupId> <artifactId>azure-functions-maven-plugin</artifactId> <version>${azure.functions.maven.plugin.version}</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.3.0</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.4.0</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-clean-plugin</artifactId> </plugin> <plugin> <groupId>io.gatling</groupId> <artifactId>gatling-maven-plugin</artifactId> <version>4.1.5</version> <configuration> <includes> <include>com.example.loadtest.*</include> </includes> </configuration> </plugin> </plugins> </pluginManagement>
<plugins> <plugin> <groupId>com.microsoft.azure</groupId> <artifactId>azure-functions-maven-plugin</artifactId> <configuration> <resourceGroup>${functionResourceGroup}</resourceGroup> <appName>${functionAppName}</appName> <region>${functionAppRegion}</region> <appServicePlanName>${functionAppServicePlanName}</appServicePlanName> <pricingTier>${functionPricingTier}</pricingTier>
<hostJson>${project.basedir}/src/main/azure/host.json</hostJson> <localSettingsJson>${project.basedir}/src/main/azure/local.settings.json</localSettingsJson>
<runtime> <os>linux</os> <javaVersion>17</javaVersion> </runtime> <appSettings> <!-- Run Azure Function from package file by default --> <property> <name>FUNCTIONS_EXTENSION_VERSION</name> <value>~4</value> </property> </appSettings> </configuration> <executions> <execution> <id>package-functions</id> <goals> <goal>package</goal> </goals> </execution> </executions> </plugin> <!--Remove obj folder generated by .NET SDK in maven clean--> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.2.0</version> <configuration> <filesets> <fileset> <directory>obj</directory> </fileset> </filesets> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ``` **DemoApplication.java:** ```java package com.example;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication public class DemoApplication {
public static void main(String[] args) throws Exception { SpringApplication.run(DemoApplication.class, args); } } ``` **HelloHandler.java:** ```java package com.example;
import com.example.model.Greeting; import com.example.model.User; import com.microsoft.azure.functions.*; import com.microsoft.azure.functions.annotation.AuthorizationLevel; import com.microsoft.azure.functions.annotation.FunctionName; import com.microsoft.azure.functions.annotation.HttpTrigger; import org.springframework.cloud.function.adapter.azure.FunctionInvoker;
import java.util.Optional;
public class HelloHandler extends FunctionInvoker<User, Greeting> {
@FunctionName("hello") public HttpResponseMessage execute( @HttpTrigger(name = "request", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<User>> request, ExecutionContext context) { User user = request.getBody() .filter((u -> u.getName() != null)) .orElseGet(() -> new User( request.getQueryParameters() .getOrDefault("name", "world"))); context.getLogger().info("Greeting user name: " + user.getName()); return request .createResponseBuilder(HttpStatus.OK) .body(handleRequest(user, context)) .header("Content-Type", "application/json") .build(); } }
``` **Deployed the spring boot function to Azure using the command `mvn azure-functions:deploy`:** ```java C:\Users\uname\hello-spring-function-azure>mvn azure-functions:deploy [INFO] Scanning for projects... [INFO] [INFO] -------------------------< com.example:hello >-------------------------- [INFO] Building Hello Spring Function on Azure 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- azure-functions:1.22.0:deploy (default-cli) @ hello --- [INFO] Auth type: AZURE_CLI Default subscription: Subscription(158b8345XXd98-95c5-f21815dd048f) Username: userid //Removed few logs [INFO] Function App(rkfn) is successfully created [INFO] Starting deployment... [INFO] Trying to deploy artifact to rkfn... [INFO] Successfully deployed the artifact to https://functionname.azurewebsites.net [INFO] Deployment done, you may access your resource through rkfn.azurewebsites.net [INFO] Syncing triggers and fetching function information [INFO] Querying triggers... [INFO] HTTP Trigger Urls: [INFO] hello : https://functionname.azurewebsites.net/api/hello [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 02:22 min [INFO] Finished at: 2024-12-19T16:40:02+05:30 [INFO] ------------------------------------------------------------------------ ``` **Portal:**
![enter image description here](https://i.imgur.com/q7WxFCZ.png)
**Response:**
![enter image description here](https://i.imgur.com/bTjxMh1.png)
**Route to `https://functionname.azurewebsites.net/api/hello`:**
![enter image description here](https://i.imgur.com/l0n4HKX.png)

        
Present in both answers; Present only in the new answer; Present only in the old answer;