hello I installed jackson dataformat xml dependency then i remove it from xml and I did this command mvn clean install to make sure that json will become the default format but xml still the default my question is how i can turn back to json is that problem with the dependency when i install it it can’t removed ?
package com.example.rest.webservices.restfulwebservices.user;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import java.net.URI;
import java.util.List;
@RestController
public class UserResource {
private UserDaoService service;
private Logger logger;
public UserResource(UserDaoService service) {
this.service = service;
}
@GetMapping("/users")
public List<User> RetrieveUsers() {
return service.findAll();
}
@GetMapping("/users/{id}")
public User RetrieveUser(@PathVariable int id) {
User user = service.findUser(id);
if(user==null) {
throw new UserNotFoundException("id:"+id);
}
return user;
}
@DeleteMapping("/users/{id}")
public void RemoveUser(@PathVariable int id) {
User user = service.findUser(id);
service.removeUser(id);
}
@PostMapping("/users")
public ResponseEntity<User> createUser(@Valid @RequestBody User user) {
User savedUser = service.saveUser(user);
URI location = ServletUriComponentsBuilder.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(savedUser.getId())
.toUri();
return ResponseEntity.created(location).build();
}
}
package com.example.rest.webservices.restfulwebservices.user;
import org.springframework.stereotype.Component;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
@Component
public class UserDaoService {
private static List<User> users = new ArrayList<>();
public static Integer CountUser = 0;
static {
users.add(new User(++CountUser, "youssef", LocalDate.now().minusYears(20)));
users.add(new User(++CountUser, "achraf", LocalDate.now().minusYears(25)));
users.add(new User(++CountUser, "nassim", LocalDate.now().minusYears(30)));
}
public List<User> findAll() {
return users;
}
public User findUser(int id) {
Predicate<User> predicate = user -> user.getId() == id;
return users.stream().filter(predicate).findFirst().orElse(null);
}
public User saveUser(User user) {
user.setId(++CountUser);
users.add(user);
return user;
}
public void removeUser(int id) {
Predicate<User> predicate = user -> user.getId() == id;
users.removeIf(predicate);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.rest.webservices</groupId>
<artifactId>restful-web-services</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>restful-web-services</name>
<description>restful-web-services</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The content type determine what is in the body of the request/response. See baeldung.com/spring-mvc-set-json-content-type
but before i install dataformat xml json was as a default format do i need now to do this consumes=”application/json ?
Dataformat “xml json” does not make sense. It should be either xml or json. The request and response can be different format. Yes as stated here : baeldung.com/…