Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion spring-boot-modules/spring-boot-mvc-2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ This module contains articles about Spring Web MVC in Spring Boot projects.
- [Functional Controllers in Spring MVC](https://www.baeldung.com/spring-mvc-functional-controllers)
- [Specify an Array of Strings as Body Parameters in Swagger](https://www.baeldung.com/swagger-body-array-of-strings)
- [Swagger @ApiParam vs @ApiModelProperty](https://www.baeldung.com/swagger-apiparam-vs-apimodelproperty)
- [ETags for REST with Spring](https://www.baeldung.com/etags-for-rest-with-spring)
- [Testing REST with multiple MIME types](https://www.baeldung.com/testing-rest-api-with-multiple-media-types)
- [Testing Web APIs with Postman Collections](https://www.baeldung.com/postman-testing-collections)
- [Spring Boot Consuming and Producing JSON](https://www.baeldung.com/spring-boot-json)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.baeldung.mime;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Version;
import java.io.Serializable;

@Entity
public class Foo implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

@Column(nullable = false)
private String name;

@Version
private long version;

public Foo() {
super();
}

public Foo(final String name) {
super();

this.name = name;
}

// API

public long getId() {
return id;
}

public void setId(final long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(final String name) {
this.name = name;
}

public long getVersion() {
return version;
}

public void setVersion(long version) {
this.version = version;
}

//

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}

@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Foo other = (Foo) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}

@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Foo [name=").append(name).append("]");
return builder.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.baeldung.mime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping(value = "/foos")
public class FooController {

@Autowired
private FooDao fooDao;

@GetMapping(value = "/{id}")
public Foo findById(@PathVariable("id") final Long id, final HttpServletResponse response) {
return fooDao.findById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
}

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Foo create(@RequestBody final Foo resource, final HttpServletResponse response) {
return fooDao.save(resource);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.baeldung.mime;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface FooDao extends CrudRepository<Foo, Long>{
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.baeldung.etag.Foo;
import com.baeldung.etag.WebConfig;

import io.restassured.RestAssured;
import io.restassured.response.Response;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes= WebConfig.class, webEnvironment = WebEnvironment.RANDOM_PORT)
@ComponentScan({"com.baeldung.mime", "com.baeldung.etag"})
@SpringBootTest(classes = FooController.class, webEnvironment = WebEnvironment.RANDOM_PORT)
@ComponentScan({"com.baeldung.mime"})
@EnableAutoConfiguration
@ActiveProfiles("test")
public class FooLiveTest {
Expand All @@ -47,12 +44,12 @@ protected final void create(final Foo resource) {
createAsUri(resource);
}

private final String createAsUri(final Foo resource) {
private String createAsUri(final Foo resource) {
final Response response = createAsResponse(resource);
return getURL() + "/" + response.getBody().as(Foo.class).getId();
}

private final Response createAsResponse(final Foo resource) {
private Response createAsResponse(final Foo resource) {

final String resourceAsString = marshaller.encode(resource);
return RestAssured.given()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;

import com.baeldung.etag.Foo;
import com.baeldung.mime.Foo;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import org.springframework.http.MediaType;

import com.baeldung.etag.Foo;
import com.baeldung.mime.Foo;
import com.thoughtworks.xstream.XStream;

public final class XStreamMarshaller implements IMarshaller {
Expand Down
1 change: 1 addition & 0 deletions spring-boot-modules/spring-boot-mvc-3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ This module contains articles about Spring Web MVC in Spring Boot projects.
- [Differences in @Valid and @Validated Annotations in Spring](https://www.baeldung.com/spring-valid-vs-validated)
- [CharacterEncodingFilter In SpringBoot](https://www.baeldung.com/spring-boot-characterencodingfilter)
- [HandlerInterceptors vs. Filters in Spring MVC](https://www.baeldung.com/spring-mvc-handlerinterceptor-vs-filter)
- [ETags for REST with Spring](https://www.baeldung.com/etags-for-rest-with-spring)
- More articles: [[prev -->]](/spring-boot-modules/spring-boot-mvc-2)
8 changes: 8 additions & 0 deletions spring-boot-modules/spring-boot-mvc-3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.baeldung.etag;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Version;
import java.io.Serializable;

@Entity
public class Foo implements Serializable {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.baeldung.etag;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -16,6 +14,8 @@
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping(value = "/foos")
public class FooController {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.baeldung.etag;

import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import org.assertj.core.util.Preconditions;
import org.junit.Ignore;
import org.junit.Test;
Expand All @@ -18,12 +18,10 @@
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
Expand Down