我收到以下错误:
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 字符串文件?