关于java:内容类型\’application/json;charset=UTF-8\’

Content type 'application/json;charset=UTF-8'

您好,当我使用邮递员发送帖子请求时,谁能帮我解决这个错误,这是我的控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.example.rba.controller;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.validation.annotation.Validated;
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.RestController;

import com.example.rba.model.Booking;
import com.example.rba.model.LoginResponse;
import com.example.rba.model.Room;
import com.example.rba.model.User;
import com.example.rba.repository.BookingRepository;
import com.example.rba.repository.RoomRepository;
import com.example.rba.repository.UserRepository;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

@RestController
@RequestMapping("/api")
public class Controller {

    @Autowired
    BookingRepository bookingRepository;
    @Autowired
    RoomRepository roomRepository;
    @Autowired
    private JavaMailSender sender;
    @Autowired
    UserRepository userRepository;

    public String generatedString = RandomStringUtils.randomAlphabetic(10);

    @GetMapping("/read")
    public List<Booking> read() {
        return bookingRepository.findAll();
    }

    @GetMapping("/rooms")
    public List<Room> room(){
        return roomRepository.findAll();
    }

    @PostMapping(path ="/createBooking/{location}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<String> create(@PathVariable(value ="location") String location,
            @Validated @RequestBody Booking book) {
        book.setStatus("reserved");
        book.setBookingCode(generatedString);

        bookingRepository.save(book);

        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);
        StringBuilder sb = new StringBuilder();

        try {
            String[] array = new String[book.getAtt().size()];
            int index = array.length;
            for (Object value : book.getAtt()) {
                array[index] = (String) value;
            }
            helper.setTo(array[index]);
            sb.append("Agenda:" +"" + book.getBookingDesc()).append(System.lineSeparator());
            sb.append("When:" + "" + book.getDateBooked() +"" + book.getStartTime() +"" +"To" +"" + book.getEndTime())
                    .append(System.lineSeparator());
            sb.append("Where:" +"" + location).append(System.lineSeparator());
            sb.append("By:" +"" + book.getBookedUser()).append(System.lineSeparator());
            sb.append("Booking code:" +"" + generatedString);
            helper.setText(sb.toString());
            helper.setSubject("Meeting");
        } catch (MessagingException e) {
            e.printStackTrace();
            return new ResponseEntity<>("Error while sending mail ..", HttpStatus.BAD_REQUEST);
        }
        sender.send(message);

        return new ResponseEntity<>("Inputs have been saved", HttpStatus.OK);
    }

    @PutMapping("/editEquip/{id}")
    public ResponseEntity<Room> edit(@PathVariable(value ="id") Long id, @Validated @RequestBody Room room) {
        Room editRoom = roomRepository.getOne(id);

        if (Objects.isNull(editRoom)) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }

        editRoom.setEquip(room.getEquip());

        Room newEquip = roomRepository.save(editRoom);
        return new ResponseEntity<>(newEquip, HttpStatus.OK);
    }

    @GetMapping("/getEquip")
    public List<Room> readEquip() {
        return roomRepository.findAll();
    }

    @PostMapping("/login")
    public ResponseEntity<LoginResponse> login(@Validated @RequestBody User user){
        String jwtToken ="";
        String username = user.getName();
        String password = user.getPassword();

        LoginResponse response = new LoginResponse();
        if(user.getName() == null && user.getPassword() == null || user.getName() != username && user.getPassword() != password) {
            return new ResponseEntity<>(response, HttpStatus.UNAUTHORIZED);
        }

        List<User> reg =
                userRepository.findByNameAndPassword(username, password);

        jwtToken = Jwts.builder().setSubject(username).claim("info", user)
                .setIssuedAt(new Date()).signWith(SignatureAlgorithm.HS256,"secretKey")
                .compact();

        if(Objects.isNull(reg) || reg.isEmpty()) {
            response.setStatus(false);
            response.setMessage(username +"" +"doesn't exist");
            return new ResponseEntity<>(response, HttpStatus.UNAUTHORIZED);
        }
        else {
            response.setStatus(true);
            response.setMessage("Welcome");
            response.setToken(jwtToken);
        }

        return new ResponseEntity<>(response, HttpStatus.ACCEPTED);
    }

    @PostMapping("/register")
    private ResponseEntity<String> register(@Validated @RequestBody User user) {

        user.setPassword(generatedString);

        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);
        StringBuilder sb = new StringBuilder();

        try {
            helper.setTo(user.getName());
            sb.append("Password:" +"" + user.getPassword());
            helper.setText(sb.toString());
            helper.setSubject("Registeration");
        } catch (MessagingException e) {
            e.printStackTrace();
            return new ResponseEntity<>("Error while sending mail ..", HttpStatus.BAD_REQUEST);
        }
        sender.send(message);

        userRepository.save(user);
        return new ResponseEntity<>("Registered Successfully, Please check your email for your password", HttpStatus.CREATED);
    }

}

这就是我使用邮递员发送 json 的方式

enter

我收到此错误尝试搜索如何修复它,但错误仍然存??在

"trace":"org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:225)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:158)
at

已编辑:这是邮递员的标题

enter

提前致谢

相关讨论
  • 它很难从图像中读取,最好用 stacktrace 替换图像
  • 哦,好吧,对不起
  • 您使用的是 @Controller 还是 @RestController?
  • @RestController
  • @Phil抱歉,但我对spring还是很陌生,你指的是什么标题?
  • @Phil 内容类型 = 应用程序/json
  • @Phil 我编辑了我的帖子并发布了标题
  • 您能否尝试正确编码 URL,即 http://localhost:8080/api/createBooking/Kings%20Landing...。它真的需要那些尾随点吗?这可能会扰乱 Spring 的内容协商
  • 我应该这样输入网址吗? localhost:8080/api/createBooking/Kings Landing... 而不是这个 ?localhost:8080/api/createBooking/Kings Landing...
  • 带有点 (.) 的 Spring MVC @PathVariable 的可能副本被截断。尝试使用 @PostMapping("/createBooking/{location:.+}")。是的,URL 中的空格应该编码为 %20
  • @Phil 我尝试添加 creatBooking/{location:.就像他们在你发送的链接中所说的一样,但仍然没有修复它
  • 您能否发布您的整个课程,包括进口
  • @ThomasAndolf 我编辑了我的帖子并包括了我的整个班级
  • 检查这个
  • @emotionlessbananas 尝试了所有建议仍然没有成功
  • @emotionlessbananas 哎呀抱歉,刚刚重新阅读了错误的痕迹,并尝试了您发送的链接中的建议,它有效,谢谢

问题解决了我在我的一个模型类中删除了@JsonManagedReference 并且它有效,但是该类与我正在使用的类无关我想知道为什么


添加此标签 @PostMapping(path ="/createBooking/{location}", 产生 = MediaType.APPLICATION_JSON_UTF8_VALUE,
消耗 = MediaType.APPLICATION_JSON_UTF8_VALUE)

相关讨论
  • 您好尝试了这个并导致错误提示"此行的多个标记 \t- APPLICATION_JSON_UTF8_VALUE 无法解析或 \t 不是字段"。
  • 你应该导入 org.springframework.http.MediaType;
  • 哎呀,我输入了错误的错误,但它仍然没有修复错误仍然返回 415
  • 为什么要添加 produces = MediaType.APPLICATION_JSON_UTF8_VALUE 给定 OP 的方法会产生一个纯字符串

以上是关于java:内容类型\’application/json;charset=UTF-8\’的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>