我收到以下错误:
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字符串文件?
答案 0 :(得分:1)
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();
}
这将转换为String格式。感谢所有给予支持的人。
答案 1 :(得分:0)
JAXB
@XmlRootElement
public class User {
}
JAX-RS将为您完成。
@Service
@Path("/services")
public class UserServiceImpl implements UserService {
@GET
@Path("{userName}/{password}")
@Produces(MediaType.APPLICATION_XML)
public User login(@PathParam("userName")String username,
@PathParam("password")String password)
throws JAXBException, PropertyException, FileNotFoundException {
final User user = findUser();
/**
// not even required; JAX-RS will say 404 for null
if (user == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
**/
return user;
}
}