嗨,有人能帮我处理这个错误吗?当我使用邮递员发送邮件请求时,这里是我的控制器
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);
}
}
这就是我使用postman发送json的方式
我正在尝试搜索如何修复它,但错误仍然存在
编辑:这是邮递员的标题
提前致谢
问题解决我删除了我的一个模型类中的@JSONManagedReference,它起作用了,但是那个类与我正在使用的类不相关,我想知道为什么
问题内容: 使用firebug时,我在asp.net mvc 4项目中收到此有线错误“ NetworkError:415无法处理… xt / xml; charset = utf-8’– ”。 代码: 服务代码: 该接口是: 和网络配置,我在WCF中使用了无文件: 问题答案: 您需要使用,而不是代码中的常规。这将为端点配置适当的绑定()和行为()以遵守该属性。
问题内容: 似乎JSON默认编码为UTF-8,Spring MVC 默认返回 ,很难更改。 问题答案: 根据RFC 4627 JSON文本应以Unicode编码。默认编码为UTF-8。 它继续描述了如何检测不同的UTF- *编码,这表明不支持其他编码。 “ SHALL”表示此处的绝对要求(请参阅RFC 2119)。 而且真的是没有理由使用与JSON非UTF编码(如任何可以处理JSON可以 肯定 处
@PostMapping public UserResponse createUser(@RequestBody UserRequest userDetail){ 这是我收到的错误代码 } 我尝试了很多不同的方法仍然没有得到解决方案这是来自邮递员的图片来自邮递员的图片
我试图使用独立的应用程序使用WCF web服务。我可以使用Internet Explorer查看此服务,也可以在Visual studio服务引用中查看。 这就是我得到的错误 如何更改它以使用正确的内容类型? 这是我的配置文件 这是堆栈
我的角码 你有什么想法吗?
我试图在应用程序的另一个模块中访问SpringRESTendpoint。因此,我尝试使用REST模板来获取用户列表,如下所示: 使用REST模板的API请求: LeadUserList类: LeadUser模型类: APIendpoint控制器类: 尽管返回类型是来自APIendpoint的JSON,但我得到了上述异常。我做错了什么? 例外情况: