gpt4 book ai didi

java - JAX-RS、JAXB 作为字符串 xml 文件返回

转载 作者:行者123 更新时间:2023-12-01 14:17:55 26 4
gpt4 key购买 nike

我收到以下错误:

XML Parsing Error: syntax error
Location: http://localhost:8080/assignment/services/services/test/test1
Line Number 1, Column 1:sucess
^

我的java代码是:

@Service
@Path("/services/")
public class UserServiceImpl implements UserService {

List<String> validUsers = Arrays.asList("test", "admin"); // testing purpose
List<String> validPassword = Arrays.asList("test1", "admin1");
String USER_DETAILS_XML = "./user-details.xml";
String USER_ERROR_XML = "./user-error.xml";

/**
* This method validates user login credentials based on temporary user id and password and returns the message
* @param username
* @param password
* @return user message
*/
@GET
@Path("{userName}/{password}")
@Produces(MediaType.TEXT_XML)
public String login(@PathParam("userName")String username, @PathParam("password")String password)
throws JAXBException, PropertyException, FileNotFoundException {
User user = new User();
InvalidUser invalidUser = new InvalidUser();
if ((validUsers.contains(username) && validPassword.contains(password))) {
user.setUserName(username);
user.setFirstName(getName());
user.setLastName("Tom");
return validUser(user);
}
else{
invalidUser.setCode(400);
invalidUser.setMessage("Invalid Credentials");
return invalidUser(invalidUser);
}
}

/*
* This method verifies and creates user details xml file
*/

private String validUser(User user) throws JAXBException, PropertyException, FileNotFoundException {
JAXBContext jaxbContext = JAXBContext.newInstance(User.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(user,new File(USER_DETAILS_XML));
return "sucess";
}
/*
* This method verifies and creates user error xml file
*/
private String invalidUser(InvalidUser invalidUser) throws JAXBException,PropertyException, FileNotFoundException {
JAXBContext jaxbContext = JAXBContext.newInstance(InvalidUser.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(invalidUser, new File(USER_ERROR_XML));
return "failure";
}

我想以 XML 字符串形式返回。如何将 jaxb 转换/返回为 xml 字符串文件?

最佳答案

private String validUser(User user) throws JAXBException, PropertyException, FileNotFoundException {
JAXBContext jaxbContext = JAXBContext.newInstance(User.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter stringWriter = new StringWriter();
marshaller.marshal(user,new File(USER_DETAILS_XML));
marshaller.marshal(user,stringWriter);
return stringWriter.toString();
}
/*
* This method verifies and creates user error xml file
*/
private String invalidUser(InvalidUser invalidUser) throws JAXBException,PropertyException, FileNotFoundException {
JAXBContext jaxbContext = JAXBContext.newInstance(InvalidUser.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter stringWriter = new StringWriter();
marshaller.marshal(invalidUser, new File(USER_ERROR_XML));
marshaller.marshal(invalidUser, stringWriter);
return stringWriter.toString();
}

这会转换为字符串格式。感谢每一位给予支持的人。

关于java - JAX-RS、JAXB 作为字符串 xml 文件返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17937233/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com