1 /* 2 * Copyright 2023 Code Intelligence GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example; 18 19 import org.springframework.boot.SpringApplication; 20 import org.springframework.boot.autoconfigure.SpringBootApplication; 21 import org.springframework.web.bind.annotation.GetMapping; 22 import org.springframework.web.bind.annotation.PostMapping; 23 import org.springframework.web.bind.annotation.RequestBody; 24 import org.springframework.web.bind.annotation.RequestParam; 25 import org.springframework.web.bind.annotation.RestController; 26 27 @SpringBootApplication 28 @RestController 29 public class JunitSpringWebApplication { 30 public static final class HelloRequest { 31 public static final HelloRequest DEFAULT = new HelloRequest(); 32 33 String prefix = "Hello "; 34 String name = "World"; 35 String suffix = "!"; 36 getPrefix()37 public String getPrefix() { 38 return prefix; 39 } 40 setPrefix(String prefix)41 public void setPrefix(String prefix) { 42 this.prefix = prefix; 43 } 44 getName()45 public String getName() { 46 return name; 47 } 48 setName(String name)49 public void setName(String name) { 50 this.name = name; 51 } 52 getSuffix()53 public String getSuffix() { 54 return suffix; 55 } 56 setSuffix(String suffix)57 public void setSuffix(String suffix) { 58 this.suffix = suffix; 59 } 60 } 61 62 @GetMapping("/hello") sayHello(@equestParamrequired = false, defaultValue = "World") String name)63 public String sayHello(@RequestParam(required = false, defaultValue = "World") String name) { 64 return "Hello " + name; 65 } 66 67 @GetMapping("/buggy-hello") buggyHello(@equestParamrequired = false, defaultValue = "World") String name)68 public String buggyHello(@RequestParam(required = false, defaultValue = "World") String name) 69 throws Error { 70 if (name.equals("error")) { 71 throw new Error("Error found!"); 72 } 73 return "Hello " + name; 74 } 75 76 @PostMapping("/hello") postHello(@equestBody HelloRequest request)77 public String postHello(@RequestBody HelloRequest request) { 78 if ("error".equals(request.name)) { 79 throw new Error("Error found!"); 80 } 81 return request.prefix + request.name + request.suffix; 82 } 83 main(String[] args)84 public static void main(String[] args) { 85 SpringApplication.run(JunitSpringWebApplication.class, args); 86 } 87 } 88