Skip to content

Commit

Permalink
release 1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
per committed Feb 11, 2023
1 parent 60b4bd0 commit b0a8b03
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 10 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Add the following dependency to your maven pom.xml (or equivalent for your build
<dependency>
<groupId>se.alipsa</groupId>
<artifactId>simple-rest</artifactId>
<version>1.0.3</version>
<version>1.0.4</version>
</dependency>
```

Expand Down Expand Up @@ -137,6 +137,22 @@ List<Company> getCompanies(String jwtToken) {
}
```

## Complex return types
In some cases you have some even more complex type that you want to get from the response.
In these cases you create a TypeReference and pass that to the getForType method.
Here is an example:

```groovy
import se.alipsa.simplerest.*;
import com.fasterxml.jackson.core.type.TypeReference;
import java.util.Map;
import java.util.List;
var restClient = new RestClient();
Response response = restClient.get("https://localhost:8080/api/info");
Map<String, List<String>> info = response.getForType(new TypeReference<>(){});
```

## Using simple-rest for REST/XML
The Rest service response is assumed to be JSON. If you need to use REST/XML you need to
1. Add a dependency to com.fasterxml.jackson.dataformat:jackson-dataformat-xml
Expand Down
16 changes: 8 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>se.alipsa</groupId>
<artifactId>simple-rest</artifactId>
<version>1.0.4-SNAPSHOT</version>
<version>1.0.4</version>
<packaging>jar</packaging>

<name>Simple Rest</name>
Expand Down Expand Up @@ -43,42 +43,42 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.13.4</version>
<version>2.14.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.4.2</version>
<version>2.14.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.13.4</version>
<version>2.14.2</version>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-reload4j</artifactId>
<version>2.0.3</version>
<version>2.0.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>11.0.12</version>
<version>11.0.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>11.0.12</version>
<version>11.0.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.1</version>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
4 changes: 3 additions & 1 deletion releases.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Release history

### ver 1.0.4, In development...
### ver 1.0.4, 2023-02-11
- Add getForType which takes a TypeReference as parameter to be able to return "anything"
- Upgrade dependencies for jackson, and test dependencies (jetty, junit, slf4j)

### ver 1.0.3, 2022-08-01
- upgrade test dependencies (junit, jetty).
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/se/alipsa/simplerest/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public <T> List<T> getObjectList(Class<T> returnClass, ObjectMapper... customMap
return mapper.readValue(getPayload(), mapper.getTypeFactory().constructCollectionType(List.class, returnClass));
}

public <T> T getForType(TypeReference<T> type, ObjectMapper... customMapper) throws JsonProcessingException {
ObjectMapper mapper = customMapper.length > 0 ? customMapper[0] : objectMapper;
return mapper.reader().forType(type).readValue(getPayload());
}

/**
*
* @return the "raw" content of the response
Expand Down
72 changes: 72 additions & 0 deletions src/test/java/test/alipsa/simplerest/ComplexRestTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package test.alipsa.simplerest;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import se.alipsa.simplerest.RestClient;
import se.alipsa.simplerest.RestException;
import test.alipsa.simplerest.servlets.ComplexServlet;

import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

public class ComplexRestTest {

private static Server server;
private static String serverUrl;
private static RestClient restClient;



@BeforeAll
public static void startJetty() throws Exception {
//System.out.println("Starting jetty server");
server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(0); // auto-bind to available port
server.addConnector(connector);

ServletContextHandler context = new ServletContextHandler();
context.addServlet(ComplexServlet.class,"/complex/*");
server.setHandler(context);

server.start();

String host = connector.getHost();
if (host == null) {
host = "localhost";
}
int port = connector.getLocalPort();
serverUrl = String.format("http://%s:%d/",host,port);
restClient = new RestClient();
}

@AfterAll
public static void stopJetty() {
try {
server.stop();
}
catch (Exception e) {
fail(e);
}
}


@Test
public void getComplexReturn() throws RestException, JsonProcessingException {
var response = restClient.get(serverUrl + "complex");
assertEquals(200, response.getResponseCode(), "get /complex, response Code");
Map<String, List<String>> map = response.getForType(new TypeReference<>() {});
assertEquals(2, map.size());
assertEquals("bar", map.get("two").get(1));
}

}
6 changes: 6 additions & 0 deletions src/test/java/test/alipsa/simplerest/SimpleRestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import se.alipsa.simplerest.RestClient;
import se.alipsa.simplerest.RestException;
import test.alipsa.simplerest.model.Company;
import test.alipsa.simplerest.servlets.ComplexServlet;
import test.alipsa.simplerest.servlets.SimpleServlet;

import java.util.List;
Expand Down Expand Up @@ -68,6 +69,11 @@ public void simpleGetTest() throws RestException, JsonProcessingException {
assertEquals(123, company.getNumber(), "company number");
}

@Test
public void getComplexReturn() {

}

@Test
public void simplePostTest() throws RestException, JsonProcessingException {
Company company = new Company();
Expand Down
88 changes: 88 additions & 0 deletions src/test/java/test/alipsa/simplerest/servlets/ComplexServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package test.alipsa.simplerest.servlets;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import se.alipsa.simplerest.MediaType;
import test.alipsa.simplerest.model.Company;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ComplexServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//System.out.println("In SimpleServlet.doGet for " + req.getRequestURI());
resp.setStatus(200);
resp.setContentType(MediaType.APPLICATION_JSON.getValue());
var writer = resp.getWriter();
ObjectMapper mapper = new ObjectMapper();
Map<String, List<String>> map = new HashMap<>();
map.put("one", List.of("a", "b", "c"));
map.put("two", List.of("foo", "bar", "baz"));
String value = mapper.writeValueAsString(map);
resp.setContentLength(value.getBytes(StandardCharsets.UTF_8).length);
writer.print(value);
writer.close();
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//System.out.println("In SimpleServlet.doPost for " + req.getRequestURI());
resp.setStatus(200);
resp.setContentType(MediaType.APPLICATION_JSON.getValue());
ObjectMapper mapper = new ObjectMapper();
var company = mapper.readValue(req.getInputStream(), Company.class);
company.setNumber(191919);
var writer = resp.getWriter();
writer.print(mapper.writeValueAsString(company));
writer.close();
}

@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//System.out.println("In SimpleServlet.doPut for " + req.getRequestURI());
resp.setStatus(200);
resp.setContentType(MediaType.APPLICATION_JSON.getValue());
ObjectMapper mapper = new ObjectMapper();
var company = mapper.readValue(req.getInputStream(), Company.class);
var writer = resp.getWriter();
writer.print(mapper.writeValueAsString(company));
writer.close();
}

@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//System.out.println("In SimpleServlet.doDelete for " + req.getRequestURI());
if (req.getRequestURI().endsWith("/company/123")) {
resp.setStatus(204);
} else {
resp.setStatus(404);
}
resp.setContentLength(0);
}

@Override
protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//System.out.println("In SimpleServlet.doHead for " + req.getRequestURI());
resp.setStatus(200);
resp.setContentType(MediaType.APPLICATION_JSON.getValue());
ObjectMapper mapper = new ObjectMapper();
Company company = new Company("ABC", 123);
String value = mapper.writeValueAsString(company);
resp.setContentLength(value.getBytes(StandardCharsets.UTF_8).length);
}

@Override
protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//System.out.println("In SimpleServlet.doOptions for " + req.getRequestURI());
resp.setContentType(MediaType.APPLICATION_JSON.getValue());
super.doOptions(req, resp);
}
}

0 comments on commit b0a8b03

Please sign in to comment.