在 Spring Boot 3 中默认关闭 XML

turn off xml by default in spring boot 3

提问人:cynthsbeath 提问时间:11/13/2023 最后编辑:M. Deinumcynthsbeath 更新时间:11/13/2023 访问量:32

问:

您好,我安装了杰克逊数据格式xml依赖项,然后我将其从xml中删除,然后我执行了此命令mvn clean install以确保json将成为默认格式,但xml仍然是默认格式,我的问题是我如何变回json,当我安装它时,依赖项是否有问题,无法删除?

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>
json xml spring-boot maven

评论

0赞 jdweng 11/13/2023
内容类型决定了请求/响应正文中的内容。查看 baeldung.com/spring-mvc-set-json-content-type
0赞 cynthsbeath 11/13/2023
但是在我安装 DataFormat XML JSON 之前,作为默认格式,我现在需要这样做吗 consumes=“application/json ?
0赞 jdweng 11/13/2023
数据格式“xml json”没有意义。它应该是 xml 或 json。请求和响应可以是不同的格式。是的,如这里所述:baeldung.com/...

答: 暂无答案