initial
This commit is contained in:
commit
74ae93a96e
1 changed files with 94691 additions and 0 deletions
122
joplin notion extension/Web Development/Java/Spring/Extras.md
Normal file
122
joplin notion extension/Web Development/Java/Spring/Extras.md
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
Extras
|
||||
- parsing json using jackson
|
||||
https://mkyong.com/java/jackson-how-to-parse-json/
|
||||
[How to parse JSON string with Jackson - Mkyong.com.pdf](../../../../_resources/How%20to%20parse%20JSON%20string%20with%20Jackson%20-%20Mkyong.com.pdf)
|
||||
[How to parse JSON string with Jackson - Mkyong.com (22_9_2024 8_29_12 am).html](../../../../_resources/How%20to%20parse%20JSON%20string%20with%20Jackson%20-%20Mkyong.com.html)
|
||||
|
||||
|
||||
|
||||
|
||||
```java
|
||||
|
||||
package com.example.demo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.json.*;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
public class DemoApplication {
|
||||
|
||||
|
||||
public static void main(String[] args) throws Exception{
|
||||
SpringApplication.run(DemoApplication.class, args);
|
||||
ObjectMapper objectMapper= new ObjectMapper();
|
||||
JsonNode obj=objectMapper.readTree("[\"java is oops\", \"java is not oops\", \"java is annoying\"]");
|
||||
if(obj.isArray()){
|
||||
for(int i=0;i<obj.size();i++){
|
||||
System.out.println(obj.get(i).asText());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
```
|
||||
- for json array to `List<String>`
|
||||
```java
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
List<String> options= objmapper.readValue(q.getOptions(), objmapper.getTypeFactory().constructCollectionType(List.class, String.class));
|
||||
```
|
||||
- spring cors
|
||||
https://spring.io/guides/gs/rest-service-cors
|
||||
```java
|
||||
package com.example.restservicecors;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class GreetingController {
|
||||
|
||||
private static final String template = "Hello, %s!";
|
||||
|
||||
private final AtomicLong counter = new AtomicLong();
|
||||
@CrossOrigin(origins = "http://localhost:9000")
|
||||
@GetMapping("/greeting")
|
||||
public Greeting greeting(@RequestParam(required = false, defaultValue = "World") String name) {
|
||||
System.out.println("==== get greeting ====");
|
||||
return new Greeting(counter.incrementAndGet(), String.format(template, name));
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
Array.map equivalent in java
|
||||
```java
|
||||
List<Integer> correctOptions = questionsResponses.stream().map(questionsResponse-> questionsResponse.getCorrectOptionNumber()).toList();
|
||||
```
|
||||
- converting biginteger into int
|
||||
`int questionID = ((BigInteger) questionResponseMap.get("questionID")).intValue();`
|
||||
- when using dependency
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security</artifactId>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
by default it adds authentication layer for accessing rest api, if that is not needed then it can be disabled by adding following bean
|
||||
```java
|
||||
package com.example.demo; // Adjust the package name as needed
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
public class SecurityConfig {
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
||||
http
|
||||
.authorizeRequests()
|
||||
.anyRequest().permitAll() // Allow all requests
|
||||
.and()
|
||||
.csrf().disable(); // Optionally disable CSRF protection
|
||||
|
||||
return http.build();
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
- configure PORT
|
||||
https://www.baeldung.com/spring-boot-change-port
|
||||
in application.properties
|
||||
`server.PORT=8081`
|
||||
|
||||
Loading…
Reference in a new issue