Skip to content
Open
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
4 changes: 4 additions & 0 deletions spring-web-modules/spring-mvc-basics-2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@
<artifactId>json</artifactId>
<version>${json.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.baeldung.spring.enums;
package com.baeldung.spring.enums.controller;

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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baeldung.spring.enums.entity.ModeEntity;
import com.baeldung.spring.model.Modes;

@RestController
Expand All @@ -21,4 +24,10 @@ public String getStringToMode(@RequestParam("mode") Modes mode) {
public String findByEnum(@PathVariable Modes mode) {
return "good";
}

@PostMapping("/create-modes")
public String create(@RequestBody ModeEntity entity) {
return "created";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.baeldung.spring.enums.entity;

import com.baeldung.spring.model.Modes;

public class ModeEntity {

private Long id;

private String name;

private Modes mode;

public Long getId() {
return id;
}

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

public String getName() {
return name;
}

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

public Modes getMode() {
return mode;
}

public void setMode(Modes mode) {
this.mode = mode;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
package com.baeldung.spring.model;

import com.fasterxml.jackson.annotation.JsonCreator;

public enum Modes {
ALPHA, BETA;
ALPHA("A"),
BETA("B");

private final String text;

Modes(String text) {
this.text = text;
}

public String getText() {
return this.text;
}

@JsonCreator
public static Modes fromText(String text) {
for (Modes modes : Modes.values()) {
if (modes.getText()
.equals(text)) {
return modes;
}
}
throw new IllegalArgumentException("Unknown mode value: " + text);
}

@Override
public String toString() {
return text;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ attachment.invoice=path_to_file
spring.mail.templates.path=mail-templates
#spring.mail.templates.path=/path/to/templates



Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.baeldung.spring.enums.entity;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import com.baeldung.spring.model.Modes;
import com.fasterxml.jackson.databind.ObjectMapper;

class ModeDeserializationUnitTest {


private final ObjectMapper objectMapper = new ObjectMapper();

@Test
void whenJsonContainsA_thenDeserializesToAlpha() throws Exception {
ModeEntity entity = objectMapper.readValue("{\"mode\": \"A\"}", ModeEntity.class);

assertEquals(Modes.ALPHA, entity.getMode());
}
}