Merge pull request #7730 from sjmillington/core-java-move-1

[BAEL-13505] Move articles out of core-java part 1
This commit is contained in:
Loredana Crusoveanu 2019-09-14 14:59:46 +03:00 committed by GitHub
commit 81da8bb2d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
32 changed files with 75 additions and 1 deletions

View File

@ -1,3 +1,8 @@
## Relevant Articles: ## Relevant Articles:
- [Will an Error Be Caught by Catch Block in Java?](https://www.baeldung.com/java-error-catch) - [Will an Error Be Caught by Catch Block in Java?](https://www.baeldung.com/java-error-catch)
- [Java Global Exception Handler](http://www.baeldung.com/java-global-exception-handler)
- [Common Java Exceptions](http://www.baeldung.com/java-common-exceptions)
- [Throw Exception in Optional in Java 8](https://www.baeldung.com/java-optional-throw-exception)
- [How to Find an Exceptions Root Cause in Java](https://www.baeldung.com/java-exception-root-cause)
- [Java Try with Resources](https://www.baeldung.com/java-try-with-resources)

View File

@ -26,10 +26,30 @@
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version> <version>${commons-lang3.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh-core.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh-generator-annprocess.version}</version>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<properties> <properties>
<commons-lang3.version>3.9</commons-lang3.version> <commons-lang3.version>3.9</commons-lang3.version>
<jmh-core.version>1.19</jmh-core.version>
<assertj-core.version>3.10.0</assertj-core.version>
<jmh-generator-annprocess.version>1.19</jmh-generator-annprocess.version>
</properties> </properties>
</project> </project>

View File

@ -1,5 +1,7 @@
### Relevant Articles ### Relevant Articles
- [Checking if a URL Exists in Java](https://www.baeldung.com/java-check-url-exists) - [Checking if a URL Exists in Java](https://www.baeldung.com/java-check-url-exists)
- [Making a JSON POST Request With HttpURLConnection](https://www.baeldung.com/httpurlconnection-post)
- [Using Curl in Java](https://www.baeldung.com/java-curl)
- [Do a Simple HTTP Request in Java](http://www.baeldung.com/java-http-request) - [Do a Simple HTTP Request in Java](http://www.baeldung.com/java-http-request)
- [Sending Emails with Java](http://www.baeldung.com/java-email) - [Sending Emails with Java](http://www.baeldung.com/java-email)

View File

@ -0,0 +1,46 @@
package com.baeldung.urlconnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class PostJSONWithHttpURLConnection {
public static void main (String []args) throws IOException{
//Change the URL with any other publicly accessible POST resource, which accepts JSON request body
URL url = new URL ("https://reqres.in/api/users");
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; utf-8");
con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);
//JSON String need to be constructed for the specific resource.
//We may construct complex JSON using any third-party JSON libraries such as jackson or org.json
String jsonInputString = "{\"name\": \"Upendra\", \"job\": \"Programmer\"}";
try(OutputStream os = con.getOutputStream()){
byte[] input = jsonInputString.getBytes("utf-8");
os.write(input, 0, input.length);
}
int code = con.getResponseCode();
System.out.println(code);
try(BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))){
StringBuilder response = new StringBuilder();
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
System.out.println(response.toString());
}
}
}

View File

@ -1,6 +1,7 @@
## Relevant Articles ## Relevant Articles
- [Void Type in Java](https://www.baeldung.com/java-void-type) - [Void Type in Java](https://www.baeldung.com/java-void-type)
- [Retrieve Fields from a Java Class Using Reflection](https://www.baeldung.com/java-reflection-class-fields)
- [Method Parameter Reflection in Java](http://www.baeldung.com/java-parameter-reflection) - [Method Parameter Reflection in Java](http://www.baeldung.com/java-parameter-reflection)
- [Guide to Java Reflection](http://www.baeldung.com/java-reflection) - [Guide to Java Reflection](http://www.baeldung.com/java-reflection)
- [Call Methods at Runtime Using Java Reflection](http://www.baeldung.com/java-method-reflection) - [Call Methods at Runtime Using Java Reflection](http://www.baeldung.com/java-method-reflection)